I would like to find the maximum value of column B AND keep all lines where column B values are 20% or more of the maximum.INPUT DATAA B C D E2 79 56 SD L1 09 67 JK S9 60 37 KD G0 10 47 SO EDESIRED OUTPUTA B C D E2 79 56 SD L9 60 37 KD GI have tried using awk 'BEGIN {max = 0} {if ($2>max) max=$2} END {if ($2 >= (0.1*max)) print}' file_in > file_out but this only prints out what seems to be the last line of my file.
Take maximum value from a column and extract all lines with at least 20% of this value
linux;shell script;awk
You need to save all the lines in an array to be able to go through them again at the END{ }. Or, alternatively, scan through the file twice. So, saving all the values and lines:awk 'NR == 1 {header=$0; next}            # save the header              { lines[NR]  = $0; values[NR] = $2;     # save the line and 2nd field    if ($2 > max) max = $2; }             # update max  END { print header;                     # in the end, print the header        for (i = 1 ; i <= NR ; i++)  {    # (we skipped line 0)          if (values[i] >= max * 0.2)     # print lines where $2 was high enough            print lines[i]; } } ' file_in 