我有一个文本文件。它包含这样的一行:-
I don't want to see this line 1 and next line.
exception: 14
I want to see this line 1 and next line.
exception: 16
I don't want to see this line 2 and next line.
exception: 13
I want to see this line 2 and next line.
exception: 19
我想要这样的输出:-
I want to see this line 1 and next line.
exception: 16
I want to see this line 2 and next line.
exception 19
我试图用awk解决它。但是不能得到正确的结果。 我的awk脚本:
$ cat test.awk
awk '
{if ( $2 > 14 ) print prev $0; next}
' test.txt
结果:
$ ./test.awk
I don't want to see this line 1 and next line.
I want to see this line 1 and next line.
exception: 16
I don't want to see this line 2 and next line.
I want to see this line 2 and next line.
exception: 19
答案 0 :(得分:0)
如果您想为此使用awk,那么您会遗漏一些小东西,而不必定义prev
:
awk '/^exception:/ && ($NF > 14) {print prev; print $0} {prev = $0}' file
您还应该说您的行应以exception:
开头,否则,当您有这样的行时可能会遇到问题
exception: 2
Some 15 years ago I had an exception
exception: 1
这将打印出错误的内容。
否则,请使用grep(请参阅How do you grep a file and get the next 5 lines)