如何在 Unix 命令行中搜索文件内的模式并删除这些行?

发布于 2024-08-04 21:32:38 字数 535 浏览 3 评论 0原文

我需要在文件中搜索模式。 例如文件的内容如下:

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!

这里我想搜索带有 !D! 的行并且该行中的第 7 个字段小于系统日期,那么我想删除该行并保存文件。

这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陪你到最终 2024-08-11 21:32:38

像这样的东西应该可以解决问题...如果这不是您设置字段格式的方式,您可能需要解析时间

perl -ne '/^([^!]+!){6}([^!]+).*/; print if $2 < time && /!D!/;'

Something like this should do the trick... you may want to parse the time if this is not how you have the field formatted

perl -ne '/^([^!]+!){6}([^!]+).*/; print if $2 < time && /!D!/;'
懵少女 2024-08-11 21:32:38

如果您更喜欢AWK...

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

其中logstrip.awk看起来

# *** 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
}

应该可以解决问题。

但请注意,您的字段 #7 包含奇数日期格式。我想我认识到最近的纪元值(123...),但它前面有 4 个显然不相关的数字。在与 StopDate 进行比较之前,可以轻松删除这些内容

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

神也荒唐 2024-08-11 21:32:38

基于 mjv 的答案,但进行了简化并使用(假设)第五个字段作为日期(为了可读性分为两行):

awk -F! 'BEGIN {stopdate=strftime("%Y%m%d%H%M%S",systime())} 
         $2 != "D" || $5 >= stopdate {print}' file.log > newfile.log

Based on mjv's answer, but simplified and using (assuming) the fifth field for the date (broken into two lines for readability):

awk -F! 'BEGIN {stopdate=strftime("%Y%m%d%H%M%S",systime())} 
         $2 != "D" || $5 >= stopdate {print}' file.log > newfile.log
浪荡不羁 2024-08-11 21:32:38

我用文件中的示例数据进行了测试

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!20090912000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090912000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!D!16042296!DUMMY!20090805235959!0!20090917000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090919000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090914000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090915000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090913000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090912000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090912000000!0!336344324!1!1!POST!USAGE!336344324!0!

,但它正在删除包含 !D! 的所有行。
我使用了以下 awk 脚本

# *** 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 = "date +%Y%m%d%H%M%S";
         # stopDate = 47001231000001;  for test purposes
         deletedLineCtr = 0;   #diagnostics counter, unused at this time
      }
      {
      if ( match($2, "D") && ($7 < stopDate) )
          {
           deletedLineCtr++;
          }
      else
           print $0
      }

我做错了什么吗?

i tested with the sample data in a file

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!20090912000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090912000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!D!16042296!DUMMY!20090805235959!0!20090917000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090919000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090914000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090915000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090913000000!0!336344324!1!1!POST!vijay!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090912000000!0!336344324!1!1!POST!USAGE!336344324!0!
3555005!C!336344324!1!!!EUR!1!1!!I!
3555005!S!00630000078!20090805172515!LF010300!
3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!
3555005!D!16042296!DUMMY!20090805235959!0!20090912000000!0!336344324!1!1!POST!USAGE!336344324!0!

but it's is deleting all the lines which consists of !D!.
I used the following awk script

# *** 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 = "date +%Y%m%d%H%M%S";
         # stopDate = 47001231000001;  for test purposes
         deletedLineCtr = 0;   #diagnostics counter, unused at this time
      }
      {
      if ( match($2, "D") && ($7 < stopDate) )
          {
           deletedLineCtr++;
          }
      else
           print $0
      }

Am I doing anything wrong?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文