I want to distinct print the lines where diff is minimum. (diff=$2-$3)The input file is:  c1,5,2         <-- diff=3c1,5,3         <-- diff=2c1,5,1         <-- diff=4c2,8,3         <-- diff=5c2,8,4         <-- diff=4The expected output is:   c1,5,3  c2,8,4  How can I do this with Awk (e.g. with a one-liner)?In other words, for each first field value (c1, c2), I want to print a single line with that first field value, chosen for the minimal diff between the second and third fields.
awk: print one line per field-1 value (distinct) where difference field-2 - field-3 is minimum
awk
Enjoy awk solution:awk -F, '{ diff=$2-$3; if(a[$1]>diff || !a[$1]) { a[$1]=diff; b[$1]=$2 FS $3 } }         END{ for(i in a) print i,b[i] }' OFS=',' yourfileThe output:c1,5,3c2,8,4