awk sort based on difference of fields and print all fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk sort based on difference of fields and print all fields
# 1  
Old 08-21-2015
awk sort based on difference of fields and print all fields

Hi

I have a file as below

Code:
<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.


Code:
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.

Last edited by vgersh99; 08-21-2015 at 03:44 PM.. Reason: code tags, please
# 2  
Old 08-21-2015
it's difficult to help without seeing sample input and the desired output, so shooting in the dark here:
Code:
awk '$0=($5-$7) OFS $0' myFile | sort -n -k1,1 | cut -d ' ' -f2-


Last edited by vgersh99; 08-21-2015 at 06:02 PM..
# 3  
Old 08-21-2015
In perlish:
Code:
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.
# 4  
Old 08-21-2015
please ignore if you already tried, but sort -k <field_num1>,<field_num2> doesn't work?
# 5  
Old 08-22-2015
Hi kssolanki, the OP is looking for a way to sort based on the numerical difference of these two fields...
# 6  
Old 08-24-2015
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.


Code:
-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

Code:
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.

Last edited by Scrutinizer; 08-24-2015 at 05:27 AM.. Reason: CODE tags
# 7  
Old 08-24-2015
Try sort -g instead
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies

4. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

awk to print range of fields

Hi file.in and file.out are in csv format. the code I have now is, cat file.in | awk -F"," '!($1$2$3$4$5$6$7$8 in a){a;print $0}' > file.out Here, I am printing entire line using $0. however, I want to print $1 to $150 and it should be in csv format. Cut -d is not good in performace.... (3 Replies)
Discussion started by: krishnix
3 Replies

7. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

awk /nawk :: print the everything except the first and the last fields

format of file1 "file1.txt" 1 2 3 4 A B C XX YY ZZ AA WWW The output must contain except the first and last column the output must be 2 3 B YY ZZ AA (8 Replies)
Discussion started by: centurion_13
8 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. Shell Programming and Scripting

Perl function to sort a file based on key fields

Hi, I am new to PERL.I want to sort all the lines in a file based on 1,2 and 4th filelds. Can U suggest me a command/function in perl for this operation.. (5 Replies)
Discussion started by: karthikd214
5 Replies
Login or Register to Ask a Question