本文介绍了如何搜索的文件中的图案,并删除在Unix的行中的命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要寻找在文件的图形。
例如该文件的内容是如下:

<$p$p><$c$c>3555005!K!00630000078!C!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!d!16042296!虚设!20090805235959!0!470012.31亿!0!336344324!1!1!POST!用法!336344324!0!
3555005!!C 336344324!1!EUR!1!1!我!
3555005!小号!00630000078!20090805172515!LF010300!

在这里,我想搜索与D!行!并在该行的第7场小于系统日期,然后我想删除该行并保存文件。

这可能吗?


解决方案

如果您preFER AWK ...

 的awk -f logstrip.awk in.log&GT; out.log

在这里logstrip.awk看起来像

 #***简单的awk脚本删除日志文件中的行***
#规则:把所有线路除了这些有他们的第二
#字段等于D和他们的第7字段超过
#当前日期时间
开始 {
    !FS =; #delimiter    stopDate = SYSTIME();
    #stopDate = 47001231000001;为测试目的    deletedLineCtr = 0; #diagnostics柜台,此时未使用
}{
  如果(匹配($ 2,D)及及($ 7 LT; stopDate)){
    deletedLineCtr ++;
  }
  其他
     打印$ 0个
}

应该做的伎俩。

注意,但是,你的领域#7包含奇数日期格式。我觉得我认识一个最近划时代值(123 ...),但它是由4个明显无关位pceded $ P $。这些可以很容易地比较StopDate之前删除

I need to search for a pattern in files. For example the content of the file is below:

3555005!K!00630000078!C!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!

Here I want to search for lines with !D! and the 7th field in the line is less than the system date, then I want to delete the line and save the file.

Is that possible?

解决方案

If you prefer AWK...

awk -f logstrip.awk  in.log > out.log

where logstrip.awk looks something like

# *** Simple AWK script to delete lines from log file ***
#    Rule: keep all lines except these that have their 2nd
#          field equal to "D" and their 7th field more than
#          current date time


BEGIN {
    FS = "!";   #delimiter

    stopDate = systime();
    # stopDate = 47001231000001;   for test purposes

    deletedLineCtr = 0;   #diagnostics counter, unused at this time
}

{
  if (match($2, "D") && ($7 < stopDate) ) {
    deletedLineCtr++;
  }
  else
     print $0
}

should do the trick.

Attention, however, your field #7 contains an odd date format. I think I recognize an recent epoch value (123...) but it is preceded by 4 apparently unrelated digit. These can easily be removed before comparing to StopDate

这篇关于如何搜索的文件中的图案,并删除在Unix的行中的命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:34