删除带有图案的线条
您好,我想从文件中删除与特定模式匹配的行 我正在使用的代码是
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
awk(1) 作业是在星期二截止吗?真的吗,阿克?? :-)
好吧,我不确定你到底想要什么,所以我做了一些猜测。该 awk 程序获取一天中的当前时间,然后删除文件中小于该时间的每一行。我留下了一份调试打印。
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.
首先调用
date
将格式化的日期作为参数传递:call
date
first to pass the formatted date as a parameter:您可能需要运行 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 ingawk
provides you with the formatting you want (check 'info gawk' as I did).With that fixed, you should be getting the right lines deleted.