Print previous line if condition is met

后端 未结 4 1056

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.

Ex:

In

4条回答
  •  感情败类
    2021-02-08 10:49

    This can be a way:

    $ awk '$1=="BB" && $2>1 {print f} {f=$1}' file
    AAAAAAAAAAAAA
    

    Explanation

    • $1=="BB" && $2>1 {print f} if the 1st field is exactly BB and 2nd field is bigger than 1, then print f, a stored value.
    • {f=$1} store the current line in f, so that it is accessible when reading the next line.

提交回复
热议问题