Sort a group of rows

I have a text file as 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] 

a combination of three lines forms a unit:

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

- one unit.

iv_destination_code_9

9 indicates the number by which I should sort 10 9 8 ....

I need a script / awk shell that sorts units in descending order. how is this possible?

0
source share
3 answers
 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 
+2
source
 cat file | tr '\n' '#' | sed 's/]#/]\n/g' | sort -nrt_ -k4 | tr '#' '\n' 

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

Then, in the fourth field, numerical reverse sorting is performed with fields separated by _ .

Finally, the original end of the lines is retrieved.

+3
source

This works similarly to mouvicel's , but uses non-printable characters as special markers (and assumes that the source file does not contain them).

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

It is assumed that there are no empty lines in the source file, since they delete them at the end. It also relies on every line ending with a line ending, to the end in "]".

0
source

Source: https://habr.com/ru/post/1275514/


All Articles