删除带有图案的线条

发布于 2024-08-04 13:45:21 字数 422 浏览 3 评论 0原文

您好,我想从文件中删除与特定模式匹配的行 我正在使用的代码是

BEGIN {
       FS = "!";
       stopDate = "date +%Y%m%d%H%M%S";
       deletedLineCtr = 0;   #diagnostics counter, unused at this time
      }
      {
     if( $7 < stopDate )
          {
           deletedLineCtr++;
          }
      else
           print $0
      }

该代码表示​​该文件有行“!”分隔的第 7 个字段是日期 yyyymmddhhmmss 格式。该脚本删除日期小于系统日期的行。但这行不通。谁能告诉我原因吗?

Hi I want to delete a line from a file which matches particular pattern
the code I am using is

BEGIN {
       FS = "!";
       stopDate = "date +%Y%m%d%H%M%S";
       deletedLineCtr = 0;   #diagnostics counter, unused at this time
      }
      {
     if( $7 < stopDate )
          {
           deletedLineCtr++;
          }
      else
           print $0
      }

The code says that the file has lines "!" separated and 7th field is a date yyyymmddhhmmss format. The script deletes a line whose date is less than the system date. But this doesn't work. Can any one tell me the reason?

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

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

发布评论

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

评论(3

可是我不能没有你 2024-08-11 13:45:21

awk(1) 作业是在星期二截止吗?真的吗,阿克?? :-)

好吧,我不确定你到底想要什么,所以我做了一些猜测。该 awk 程序获取一天中的当前时间,然后删除文件中小于该时间的每一行。我留下了一份调试打印。

BEGIN {
  FS = "!"
  stopDate = strftime("%Y%m%d%H%M%S")
  print "now: ", stopDate
}
{ if ($7 >= stopDate) print $0 }

$ cat t2.data
!!!!!!20080914233848
!!!!!!20090914233848
!!!!!!20100914233848
$ awk -f t2.awk < t2.data
now:  20090914234342
!!!!!!20100914233848
$ 

Is the awk(1) assignment due Tuesday? Really, awk?? :-)

Ok, I wasn't sure exactly what you were after so I made some guesses. This awk program gets the current time of day and then removes every line in the file less than that. I left one debug print in.

BEGIN {
  FS = "!"
  stopDate = strftime("%Y%m%d%H%M%S")
  print "now: ", stopDate
}
{ if ($7 >= stopDate) print $0 }

$ cat t2.data
!!!!!!20080914233848
!!!!!!20090914233848
!!!!!!20100914233848
$ awk -f t2.awk < t2.data
now:  20090914234342
!!!!!!20100914233848
$ 
一个人的旅程 2024-08-11 13:45:21

首先调用 date 将格式化的日期作为参数传递:

awk -F'!' -v stopdate=$( date +%Y%m%d%H%M%S ) '
    $7 < stopdate { deletedLineCtr++; next }
    {print}
    END {do something with deletedLineCrt...}
'

call date first to pass the formatted date as a parameter:

awk -F'!' -v stopdate=$( date +%Y%m%d%H%M%S ) '
    $7 < stopdate { deletedLineCtr++; next }
    {print}
    END {do something with deletedLineCrt...}
'
把时间冻结 2024-08-11 13:45:21

您可能需要运行 date 命令(可能带有反引号)才能将日期输入 stopDate。如果您使用编写的代码打印 stopDate ,它将包含“date + ...”,而不是数字字符串。这就是你的问题的根本原因。

不幸的是...

我找不到任何证据表明反引号在任何版本的 awk(旧 awk、新 awk、GNU awk)中都有效。因此,您要么需要将代码迁移到 Perl(Perl 最初被设计为“awk 杀手” - 并且仍然包含 a2p 将 awk 脚本转换为 Perl),或者您需要重新考虑如何日期已设定。

看到@DigitalRoss的答案,gawk中的strftime()函数为您提供了您想要的格式(像我一样检查'info gawk')。

修复后,您应该删除正确的行。

You would probably need to run the date command - maybe with backticks - to get the date into stopDate. If you printed stopDate with the code as written, it would contain "date +...", not a string of digits. That is the root cause of your problem.

Unfortunately...

I cannot find any evidence that backticks work in any version of awk (old awk, new awk, GNU awk). So, you either need to migrate the code to Perl (Perl was originally designed as an 'awk-killer' - and still includes a2p to convert awk scripts to Perl), or you need to reconsider how the date is set.

Seeing @DigitalRoss's answer, the strftime() function in gawk provides you with the formatting you want (check 'info gawk' as I did).

With that fixed, you should be getting the right lines deleted.

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