views:

42

answers:

3

I have a text file like below

iv_destination_code_10
TAP310_mapping_RATERUSG_iv_destination_code_10
RATERUSG.iv_destination_code_10 = WORK.maf_feature_info[53,6]
iv_destination_code_2
TAP310_mapping_RATERUSG_iv_destination_code_2
RATERUSG.iv_destination_code_2 = WORK.maf_feature_info[1,6]
iv_destination_code_3
TAP310_mapping_RATERUSG_iv_destination_code_3
RATERUSG.iv_destination_code_3 = WORK.maf_feature_info[7,6]
iv_destination_code_4
TAP310_mapping_RATERUSG_iv_destination_code_4
RATERUSG.iv_destination_code_4 = WORK.maf_feature_info[13,6]
iv_destination_code_5
TAP310_mapping_RATERUSG_iv_destination_code_5
RATERUSG.iv_destination_code_5 = WORK.maf_feature_info[19,6]
iv_destination_code_6
TAP310_mapping_RATERUSG_iv_destination_code_6
RATERUSG.iv_destination_code_6 = WORK.maf_feature_info[29,6]
iv_destination_code_7
TAP310_mapping_RATERUSG_iv_destination_code_7
RATERUSG.iv_destination_code_7 = WORK.maf_feature_info[35,6]
iv_destination_code_8
TAP310_mapping_RATERUSG_iv_destination_code_8
RATERUSG.iv_destination_code_8 = WORK.maf_feature_info[41,6]
iv_destination_code_9
TAP310_mapping_RATERUSG_iv_destination_code_9
RATERUSG.iv_destination_code_9 = WORK.maf_feature_info[47,6]

combination of three lines form a unit:

    iv_destination_code_9
    TAP310_mapping_RATERUSG_iv_destination_code_9
    RATERUSG.iv_destination_code_9 = WORK.maf_feature_info[47,6]

is one unit.

iv_destination_code_9

9 indicates the number by which i have to sort 10 9 8....

i need a shell script/awk which will sort the units in a descending order. how is it possible?

+3  A: 
cat file | tr '\n' '#' | sed 's/]#/]\n/g' | sort -nrt_ -k4 | tr '#' '\n'

First all end of lines are replaced by #, and end of lines at the end of blocks (]#) are recreated.

Then a numeric reverse sort is performed on the fourth field with fields separated by _.

Finally, original end of lines are retrieved.

mouviciel
+1  A: 
sed 'N;N;s/\n/#/g' file |sort -t"_" -nr -k4 | sed 's|#|\n|g'

Or with gawk

awk -vRS="\niv_" -vFS="\n" 'BEGIN{t=0}
{
 m=split($1,a,"_")
 num[a[m]]
 line[a[m]] = $0
}
END{
 cmd="sort -nr"
 for(i in num){ print i |& cmd }
    close(cmd,"to")
    while((cmd |& getline m) > 0) {
        z=split(m,arr2,"\n")
    }
    close(cmd,"from")
 print line[ arr2[1] ]
 for(j=2;j<=z;j++){
    if(line[ arr2[j]] != "" ){
        print "iv_"line[ arr2[j] ]
    }
 }
}' file
ghostdog74
A: 

This works similarly to mouvicel's answer, but uses non-printing characters as the special markers (and assumes that the original file doesn't contain them).

sed 's/]$/]'$'\1''/' text_file | tr '\1' '\0' | sort -znrt_ | tr '\0' '\n' | sed '/^$/d'

It assumes that there are no blank lines in the original file since it deletes them at the end. It also relies on every group-ending line to end in "]".

Dennis Williamson