Sorting a file based on one column using Unix and Awk

awksorttext processing

I need to sort the input file according the 6th column, which is the score.

Input File:

Sc2/80  20 . A T 86 Pass N=2 F=5;U=4
Sc2/80  20 . A C 80 Pass N=2 F=5;U=4
Sc2/60  55 . G T 90 Pass N=2 F=5;U=4
Sc2/60  55 . G C 99 Pass N=2 F=5;U=4
Sc2/20  39 . C T 97 Pass N=2 F=5;U=4
Sc2/20  39 . C A 99 Pass N=2 F=5;U=4

Expected Output:

Sc2/20 39 . C T 97 Pass N=2 F=5;U=4
Sc2/20 39 . C A 99 Pass N=2 F=5;U=4
Sc2/60 55 . G T 90 Pass N=2 F=5;U=4
Sc2/60 55 . G C 99 Pass N=2 F=5;U=4
Sc2/80 20 . A T 86 Pass N=2 F=5;U=4
Sc2/80 20 . A C 80 Pass N=2 F=5;U=4

Logic:
All the even lines of the input file should be compared and ranked according to the score (Descending Order) and the corresponding odd line of the file should be printed as well with it. If any of the scores (of the even lines) are equal then we need to look at the score of the corresponding odd line and therefore, the higher score gets priority and is printed first.

Best Answer

A possible solution is to get each two lines together, sort, then split again the joined lines

awk '{ getline line; print $0, line }' input_file | 
    sort -k6,6nr -k15,15nr | 
    awk '{ $10 = "\n" $10; print }'
Related Question