Ubuntu – Deleting rows based on the column values in a file using awk command

awkcommand linescriptstext processing

I have a huge file like below. I want to delete the rows if second column is having values 60,30 etc., all these values I will get from another file in comma separated file.

position_id risk_measure_id Scenario_id value_usd
1   60  0   300.8
2   30  0   400.6
3   45  90  300.7
4   60  0   200.9
5   30  9   400.8
6   60  10  4000.9
8   20  0   5000.9

I can use below awk command to achieve it but if I have multiple values to exclude is there any easy way.

$ awk '{ if ($2!=60 && $2!=25 && $2!=30) print $0}' test.txt
position_id risk_measure_id Scenario_id value_usd
3   45  90  300.7
8   20  0   5000.9

Best Answer

Put your values in another file:

values:

60
25
30

Then read them into an array in awk:

awk 'FNR == NR {arr[$0] = 1; next} !($2 in arr)'  values test.txt

FNR == NR holds true when reading the first file, so the first block is only executed while reading the values. Because of the next, the !($2 in arr) is only executed for the second file.

Related Question