linuxawkgrep

Print previous line if condition is met


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:

Input file

AAAAAAAAAAAAA
BB  2
CCCCCCCCCCCCC
BB 0.1

Output

AAAAAAAAAAAAA

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.

Can somebody help me with grep and awk? Thanks. Any other suggestions are also welcome. Thanks.


Solution

  • This can be a way:

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

    Explanation