File in ascending order by row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File in ascending order by row
# 1  
Old 08-28-2014
File in ascending order by row

Hi All
I have a file like this:

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

Code:
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
# 2  
Old 08-28-2014
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...
Code:
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

Last edited by Skrynesaver; 08-28-2014 at 11:21 AM.. Reason: I have a few minutes to elucidate
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 08-28-2014
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?
# 4  
Old 08-28-2014
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
Code:
ID name_A 10 name_B 3 name_C 78 name_D 2

became
Code:
ID name_D 2 name_B 3 name_A 10 name_C 78

thanks





Last edited by rbatte1; 08-28-2014 at 10:18 AM.. Reason: Set LIST tags and corrected spellings
# 5  
Old 08-28-2014
Somewhat clumsy compared to the perl oneliner above... using awk however:
Code:
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]=$(i*2+1); Y[i]=$(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

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List numerically in ascending order

Hello, I am running ubuntu 16.04 and trying to list all files inside a directory, I need to sort them in ascending order. While surfing on the site, I found an old thread but somehow it did not work. Link Ascending order with sort -nk2 myfile.txt command gives below output: file... (5 Replies)
Discussion started by: baris35
5 Replies

2. Shell Programming and Scripting

How to list files in ascending order?

Hi, I need to list files in ascending order. Filenames are in format inpTDT_1, inpTDT_2, inpTDT_3 and so on. I want to list them in the ascending order based on the digit after underscore and send the output to a file. Please help (5 Replies)
Discussion started by: Neelkanth
5 Replies

3. Shell Programming and Scripting

Arrange values in ascending order

HI I have a file # vi assc values order fin 100 34 45 200 12 64 120 10 23 Here I need to check whether the values of second column"order" is arranged ascendingly Note: Always order column will be arranged either in ascending or descending order How to make it?... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

4. UNIX for Dummies Questions & Answers

Strings in ascending order

Hi, I have a sequence which has 30000 strings which looks like this >string2991 234445 >string224 470561 >string121 675386 >string4098 177229 >string8049 255838 >string8 672382 >string1115 578415 I want it to be arranged in ascending order >string8 672382 >string121... (5 Replies)
Discussion started by: siya@
5 Replies

5. Shell Programming and Scripting

File listing in Ascending order

I have a multiple file with the following name like. file_0.csv file_1.csv file_2.csv file_3.csv file_4.csv file_5.csv file_6.csv file_7.csv file_7.csv file_8.csv file_9.csv file_10.csv file_11.csv file_12.csv file_13.csv file_14.csv (2 Replies)
Discussion started by: rakesh_arxmind
2 Replies

6. UNIX for Dummies Questions & Answers

Appending a column of numbers in ascending order to a text file

I have a text file where I want to append a column of numbers in ascending orders. Input: 57 abc 25 def 32 ghi 54 jkl Output:57 abc 57 abc 1 25 def 2 32 ghi 3 54 jkl 4 How do I go about doing that? Thanks! (11 Replies)
Discussion started by: evelibertine
11 Replies

7. Shell Programming and Scripting

Ascending order

How can I check if array is in ascending order? ---------- Post updated at 01:53 PM ---------- Previous update was at 01:25 PM ---------- Done it now (0 Replies)
Discussion started by: kristinu
0 Replies

8. UNIX for Advanced & Expert Users

merge two files in ascending order

Hello Friends, I want to merge two files in ascending order on the first field. And if the first field matches sort on 3rd field i.e, TXADDR should come ahead of RXADDR . file1 9 : TXADDR : 00000000 65 : TXDATA 0000000000000011 83 : TXDATA 0000000000000012 453 :... (10 Replies)
Discussion started by: user_prady
10 Replies

9. Shell Programming and Scripting

Ascending order within text

I appreciate all the help that I've already received but am running into one problem. I can find how to add something before a file with ascending numbers but not like this. I basically have a file that looks like this: 100 101 102 103 104 I need to add the following before each line with... (5 Replies)
Discussion started by: kerpm
5 Replies

10. UNIX for Dummies Questions & Answers

Sort / ascending order

What's the command to sort a file in ascending order and redirect the output to another file? Thanks!!!!!! (1 Reply)
Discussion started by: gyik
1 Replies
Login or Register to Ask a Question