如果满足条件,则打印上一行 [英] Print previous line if condition is met
本文介绍了如果满足条件,则打印上一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想grep一个单词,然后在该行中找到第二列,并检查它是否大于一个值.是的,我要打印上一行.
I would like to grep a word and then find the second column in the line and check if it is bigger than a value. Is yes, I want to print the previous line.
例如:
输入文件
AAAAAAAAAAAAA
BB 2
CCCCCCCCCCCCC
BB 0.1
输出
AAAAAAAAAAAAA
现在,我要搜索BB,并且如果该行中的第二列(2或0.1)大于1,我想打印前一行.
Now, I want to search for BB and if the second column (2 or 0.1) in that line is bigger than 1, I want to print the previous line.
有人可以帮我grep和awk吗?谢谢.也欢迎任何其他建议.谢谢.
Can somebody help me with grep and awk? Thanks. Any other suggestions are also welcome. Thanks.
推荐答案
这可以是一种方法:
$ awk '$1=="BB" && $2>1 {print f} {f=$1}' file
AAAAAAAAAAAAA
说明
-
$1=="BB" && $2>1 {print f}
如果第一个字段恰好是BB
并且第二个字段大于1
,则打印f
,即存储的值. -
{f=$1}
将当前行存储在f
中,以便在读取下一行时可以访问. $1=="BB" && $2>1 {print f}
if the 1st field is exactlyBB
and 2nd field is bigger than1
, then printf
, a stored value.{f=$1}
store the current line inf
, so that it is accessible when reading the next line.
Explanation
这篇关于如果满足条件,则打印上一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文