AWK – Compare Records Value with Each Other

awktext processing

Consider the below file:

foo,boo,900
foo,boo,900
foo,boo,850

I need to compare the a field ($3) with the next record, if the difference is equal or more than 50, then print the record.

i.e from the sample above, $3 from second record – $3 from the third record = 50, then the output would be:

foo,boo,850

Best Answer

You can try this awk

awk -F"," 'NR != 1 { if ((x - $3) >= 50) print $0; } { x = $3 }' file

and this one if you don't want to print row if filed $1 changed:

awk -F"," 'NR != 1 { if ($1 == fc && (x - $3) >= 50) print $0; } { x = $3; fc = $1; }' file
Related Question