awk sort based on difference of fields and print all fields

Hi

I have a file as below

<field1> <field2> <field3> ... <field_num1> <field_num2>

Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields.

I tried this and it doesn't sort on the difference field .. Appreciate your help.

cat <input file> | awk '{$5-$7} END {sort} END {print $1" "$2" "$3 " "$4" "$5" "$7" "$5-$7}' | more

This only gives me just one line as output eventhough there are multiple lines in this file.

it's difficult to help without seeing sample input and the desired output, so shooting in the dark here:

awk '$0=($5-$7) OFS $0' myFile | sort -n -k1,1 | cut -d ' ' -f2-

In perlish:

perl -ane 'push @X, [ $F[N] - $F[M], $_ ]; END { print map { $_->[1] } sort { $a->[0] <=> $b->[0] } @X; }'

where N and M are the desired fields - remember the first field is $F[0] , the second is $F[1] , etc.

please ignore if you already tried, but sort -k <field_num1>,<field_num2> doesn't work?

Hi kssolanki, the OP is looking for a way to sort based on the numerical difference of these two fields...

versh99

I tried your command almost there, however sort is not proper. There are difference values of "-8.1e-11" (in 5th row) after
"-9e-12" and followed by "-8e-12" as you can see below. Also would like to see the difference column as the last column. Can you take a quick look.

-9e-12 DC: XY_CE__Q__MODE4 G:-2L quad:slmn 1.200000e-10 -> 1.290000e-10
-9e-12 DC: XY_CE__Q__MODE4 G:-2 quad:slmn 1.200000e-10 -> 1.290000e-10
-9e-12 DC: XY_CE__Q G:-2LV quad:slmn 1.850000e-10 -> 1.940000e-10
-9e-12 DC: XY_CLK__CE__HOLD_FALLING G:-2LV quad:ftmn -1.600000e-11 -> -7.000000e-12
-8.1e-11 DC: XY_CLK__Q__F_E__MODE2 G:-2L quad:slmx 3.900000e-11 -> 1.200000e-10
-8.1e-11 DC: XY_CLK__Q__F_E__MODE2 G:-2 quad:slmx 3.900000e-11 -> 1.200000e-10
-8.1e-11 DC: XY_CLK__Q__F_E__MODE3 G:-2L quad:slmx 3.900000e-11 -> 1.200000e-10
-8.1e-11 DC: XY_CLK__Q__F_E__MODE3 G:-2 quad:slmx 3.900000e-11 -> 1.200000e-10
-8e-12 DC: XY_CE__Q__MODE1 G:-2LV quad:ftmn 9.200000e-11 -> 1.000000e-10
-8e-12 DC: XY_CE__Q__MODE1 G:-3 quad:slmn 1.130000e-10 -> 1.210000e-10
-8e-12 DC: XY_CE__Q__MODE2 G:-2LV quad:ftmn 9.200000e-11 -> 1.000000e-10
-8e-12 DC: XY_CE__Q__MODE4 G:-3 quad:slmn 1.060000e-10 -> 1.140000e-10
-7.6e-11 DC: XY_CLK__Q__F_E__MODE4 G:-1 quad:slmn 1.700000e-11 -> 9.300000e-11

Thanks

---------- Post updated at 03:05 AM ---------- Previous update was at 02:56 AM ----------

derekludwig,

I tried

my_inputfile | per <your command>

Is there a way to add the difference column as the last column, without which I find it hard to verify whether the sort is properly done. Appreciate your help.

Try sort -g instead

Please use code tags as required by forum rules.

Try an adaption of vgersh99's proposal (and adapt to taste), applied on a fictious input file prepared from your above "sample":

awk '$0=sprintf("%14.12f", $5-$7) OFS $0' file1 | sort -k1,1n
-0.000000000081 DC: XY_CLK__Q__F_E__MODE2 G:-2L quad:slmx 3.900000e-11 -> 1.200000e-10
-0.000000000081 DC: XY_CLK__Q__F_E__MODE2 G:-2 quad:slmx 3.900000e-11 -> 1.200000e-10
-0.000000000081 DC: XY_CLK__Q__F_E__MODE3 G:-2L quad:slmx 3.900000e-11 -> 1.200000e-10
-0.000000000081 DC: XY_CLK__Q__F_E__MODE3 G:-2 quad:slmx 3.900000e-11 -> 1.200000e-10
-0.000000000076 DC: XY_CLK__Q__F_E__MODE4 G:-1 quad:slmn 1.700000e-11 -> 9.300000e-11
-0.000000000009 DC: XY_CE__Q G:-2LV quad:slmn 1.850000e-10 -> 1.940000e-10
-0.000000000009 DC: XY_CE__Q__MODE4 G:-2L quad:slmn 1.200000e-10 -> 1.290000e-10
-0.000000000009 DC: XY_CE__Q__MODE4 G:-2 quad:slmn 1.200000e-10 -> 1.290000e-10
-0.000000000009 DC: XY_CLK__CE__HOLD_FALLING G:-2LV quad:ftmn -1.600000e-11 -> -7.000000e-12
-0.000000000008 DC: XY_CE__Q__MODE1 G:-2LV quad:ftmn 9.200000e-11 -> 1.000000e-10
-0.000000000008 DC: XY_CE__Q__MODE1 G:-3 quad:slmn 1.130000e-10 -> 1.210000e-10
-0.000000000008 DC: XY_CE__Q__MODE2 G:-2LV quad:ftmn 9.200000e-11 -> 1.000000e-10

Had you supplied a sample input file in the first place, an immediate taylored solution would have been possible .

Thank you guys.

awk '$8=($5-$7) OFS $8' | sort -g -k8,1 | cut -d '' -f2-

Above command worked for me.

Will follow codetags in future.

To also print out the key that is used to sort:

where $, is set to the separator of your choice, in this case, a space is used.