File in ascending order by row

Hi All
I have a file like this:

ID1 ref_A 10 ref_B 30 ref_C 5
ID2 ref_F 69 ref_G 12 ref_H 5

Every ID is followed by a string(ref_X) followed by a number(every number is referred to the previous ref)
I would like to order the file like this(the column could be more, but always with the same schema- ref_X number-)

ID1 ref_C 5 ref_A 10 ref_B 30
ID2 ref_H 5 ref_G 12 ref_F 69

Hope everything is clear!
Do you have any suggestion?

Thanks

Giuliano

If I understand you correctly you wish to maintain the row order of the file then split each record into an ID followed by any number of ref=>value tupples and sort the fields on increasing value...

perl -ne 'chomp;($id,%r)=split;print $id;for (sort {$r{$a}<=>$r{$b}} keys %r){print " $_ $r{$_}"}print"\n";' tmp.txt
ID1 ref_C 5 ref_A 10 ref_B 30
ID2 ref_H 5 ref_G 12 ref_F 69

ETA explanation
To clarify the script above...
perl -ne ' use Perl stepping though each line of input and executing the following scriptlet
chomp; Strip off any newline character this means none of the strings will have a new line at the end of them
($id,%r)=split; tokenise the string assigning the first token to the $id variable and assign the rest of the tokens in name=> value pairs in %r
print $id; print the id
for (sort {$r{$a}<=>$r{$b}} keys %r){ step through the name=>value pairs in order of lowest value to highest value
print " $_ $r{$_}" print the name value pair out
}print"\n"; finally print a new line to replace the one we removed
' tmp.txt end of scriptlet and use tmp.txt as the input

1 Like

No, not everything is clear. Any attempts from your side? Show them. And, is it always the last column to be advanced, or alwas the third?

Thank Skrynesaver for the suggestion..I will try it immediately!
Hi RudiC! I am sorry but now I am not able to resolve an issue like that.I have start one month to study awk programming but really...it is very difficult!
However the file should be read like that:

  • column 1=ID (must be included in the output, in the first column)
  • column 2 PLUS column 3= the value of these column are associated, meaning that in column 2 there is a name (suppose "ref") and in column 3 there is a number
  • column 4 PLUS column 5=as above

etc...

What I would like is to reorder the file by maintaining in the first column the ID, and the other column (always paired) should be re-ordered in ascending order.
So

ID name_A 10 name_B 3 name_C 78 name_D 2

became

ID name_D 2 name_B 3 name_A 10 name_C 78

thanks

Somewhat clumsy compared to the perl oneliner above... using awk however:

awk     'function isort( A, B, n,    i, j, holdA, holdB)
                {for( i = 2 ; i <= n ; i++)
                        {holdA = A[j = i]; holdB = B[j]
                         while ( A[j-1] > holdA )
                                { j-- ; A[j+1] = A[j]; B[j+1] = B[j] }
                         A[j] = holdA; B[j] = holdB
                        }
                }

                {N=(NF-1)/2
                 for (i=1; i<=N; i++) {X=$(i*2+1); Y=$(i*2)}
                 isort (X, Y, N)
                 printf "%s ", $1
                 for (j=1; j<=N; j++) printf "%s %s ", Y[j], X[j]
                 printf "\n"
                }
        ' file
ID1 ref_C 5 ref_A 10 ref_B 30 
ID2 ref_H 5 ref_G 12 ref_F 69 
1 Like