Sort on column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort on column
# 1  
Old 06-25-2013
Sort on column

How to sort based on the 4 the column . The input data has a header and output needs to be sorted based on the 4th column rbcid.

I tried below code but not getting results
Code:
sort -u -t'|' -k4,4r file1 > file2

Code:
time|tourit|nofdays|rbcid|blank|type|value|nill|valuedesc|name
2013-05-16T00:52:31.662-04:00|12|3|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO ABLatest
2013-05-15T00:52:31.672-04:00|1|40|39693766-NA-NA||Common Stick|ESHR||Common Stock|HS AG
2013-05-14T00:52:31.672-04:00|1|45|16111278-TSX-NA||Common Stk|ESQHR||Common Stock|STANDARD REGISTER CO
2013-05-14T00:52:31.662-04:00|1|4|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO AB
2013-05-13T00:52:31.672-04:00|1|44|15277105-NA-NA||Common Stock|DOMESHR||Common Stock|CYRO AB
2013-05-10T00:52:31.672-04:00|1|5|39693766-NYSE-EUR||Common HSAGStick|ESHR||Common Stock|HS AG

# 2  
Old 06-25-2013
please post sample input file as well as expected output ...
# 3  
Old 06-25-2013
Sample Input:
Code:
time|tourit|nofdays|rbcid|blank|type|value|nill|valuedesc|name
2013-05-16T00:52:31.662-04:00|12|3|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO ABLatest
2013-05-15T00:52:31.672-04:00|1|40|39693766-NA-NA||Common Stick|ESHR||Common Stock|HS AG
2013-05-14T00:52:31.672-04:00|1|45|16111278-TSX-NA||Common Stk|ESQHR||Common Stock|STANDARD REGISTER CO
2013-05-14T00:52:31.662-04:00|1|4|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO AB
2013-05-13T00:52:31.672-04:00|1|44|15277105-NA-NA||Common Stock|DOMESHR||Common Stock|CYRO AB
2013-05-10T00:52:31.672-04:00|1|5|39693766-NYSE-EUR||Common HSAGStick|ESHR||Common Stock|HS AG

Sample Output:
Code:
time|tourit|nofdays|rbcid|blank|type|value|nill|valuedesc|name
2013-05-16T00:52:31.662-04:00|12|3|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO ABLatest
2013-05-14T00:52:31.662-04:00|1|4|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO AB
2013-05-13T00:52:31.672-04:00|1|44|15277105-NA-NA||Common Stock|DOMESHR||Common Stock|CYRO AB
2013-05-15T00:52:31.672-04:00|1|40|39693766-NA-NA||Common Stick|ESHR||Common Stock|HS AG
2013-05-10T00:52:31.672-04:00|1|5|39693766-NYSE-EUR||Common HSAGStick|ESHR||Common Stock|HS AG
2013-05-14T00:52:31.672-04:00|1|45|16111278-TSX-NA||Common Stk|ESQHR||Common Stock|STANDARD REGISTER CO

# 4  
Old 06-25-2013
Code:
head -1 file; tail -n+2 file | sort -t'|' -k4,4r
time|tourit|nofdays|rbcid|blank|type|value|nill|valuedesc|name
2013-05-10T00:52:31.672-04:00|1|5|39693766-NYSE-EUR||Common HSAGStick|ESHR||Common Stock|HS AG
2013-05-15T00:52:31.672-04:00|1|40|39693766-NA-NA||Common Stick|ESHR||Common Stock|HS AG
2013-05-14T00:52:31.672-04:00|1|45|16111278-TSX-NA||Common Stk|ESQHR||Common Stock|STANDARD REGISTER CO
2013-05-14T00:52:31.662-04:00|1|4|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO AB
2013-05-16T00:52:31.662-04:00|12|3|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO ABLatest
2013-05-13T00:52:31.672-04:00|1|44|15277105-NA-NA||Common Stock|DOMESHR||Common Stock|CYRO AB

BTW - your desired output is NOT what your sort would yield, nor any sort.
# 5  
Old 06-25-2013
copied your data into 900.txt ... ran sort -t "|" -d -k 4 900.txt after removing the first line and got this ...
Code:
2013-05-13T00:52:31.672-04:00|1|44|15277105-NA-NA||Common Stock|DOMESHR||Common Stock|CYRO AB
2013-05-14T00:52:31.662-04:00|1|4|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO AB
2013-05-16T00:52:31.662-04:00|12|3|15277105-NA-YEN||Common Stick|ESHR||Common Stock|CYRO ABLatest
2013-05-14T00:52:31.672-04:00|1|45|16111278-TSX-NA||Common Stk|ESQHR||Common Stock|STANDARD REGISTER CO
2013-05-15T00:52:31.672-04:00|1|40|39693766-NA-NA||Common Stick|ESHR||Common Stock|HS AG
2013-05-10T00:52:31.672-04:00|1|5|39693766-NYSE-EUR||Common HSAGStick|ESHR||Common Stock|HS AG

# 6  
Old 06-26-2013
Thanks.
I just wanted that the 4th column rbcid value should be sorted in such a way that all like ones are in sequence no matter what the values are.
Like rbcid value starting with 15277105 should be in a order (3 in all) one after another irrespective of the values it has. All like ones in a order.
# 7  
Old 06-26-2013
Shell script solution

I tried this and it worked for me. This could be a lengthy process but works.

Code:
tail +2 900.txt > 901.txt
awk -F"|" '{printf("%s,%d\n",NR,$4) }' 901.txt >902.txt
sort -t"," -nrk2,2 902.txt >903.txt
head -1 900.txt
while read LINE
do
 linenr=$(echo $LINE | cut -d"," -f 1)
 awk -v lnr=$linenr '(NR==lnr) {print}' 901.txt
done <903.txt

To explain the logic, the 4th field with only numeric portion is extracted into a separate file 902.txt and it is printed along with a line number. Then the data is sorted on the second field in 902.txt. The first field gives the re-ordered line numbers. Then this re-ordered line number is used to compare with original file and print corresponding row. For each line in 902.txt, the original file is read completely and only the matching line number is printed.

Last edited by krishmaths; 06-26-2013 at 04:45 AM.. Reason: Added explanation for code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort based on one column

Hi All , I am having an input file like this Input file 7 sks/jsjssj/ddjd/hjdjd/hdhd/Q 10 0.5 13 dkdkd/djdjd/djdjd/djd/QB 01 0.5 ldld/dkd/jdf/fjfjf/fjf/Q 0.5 10 sjs/jsdd/djdkd/dhd/Q 01 0.5 21 kdkd/djdd/djdd/jdd/djd/QB 01 0.5 dkdld/djdjd/djd/Q 01 0.5 ... (9 Replies)
Discussion started by: kshitij
9 Replies

2. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

3. UNIX for Dummies Questions & Answers

Sort each column independently

Hi, I have a file full of this fields and rows: 0.269330|0.035118|0.526763|0.792274 0.33555|19.471911|51.844968|1631 ... 3.981490|5.062725|17.190744|111 0.000000|0.030234|0.000000|1631 ... ... I'd like to sort EACH column individually based on the value after the third "|".... (12 Replies)
Discussion started by: a_bahreini
12 Replies

4. UNIX for Dummies Questions & Answers

Sort command in one column and not effect to another column

If my data is numerical : 1 = 101 2 = 102 3 = 104 4 = 104 7 = 103 8 = 103 9 = 105 I need the result like below: 1 = 101 2 = 102 3 = 103 4 = 103 7 = 104 8 = 104 9 = 105 (4 Replies)
Discussion started by: GeodusT
4 Replies

5. Shell Programming and Scripting

Edit column using sort

Hi Expert, Please kindly need your help, I dont have any idea how to make this input to be as output. Thanks before $ more input.dat @zmap_fault HEADER , FALT, 80, 1 X (EASTING) , 1, 1, 1, 1, 15, 7, 0.1000000E+31, , 15, 7, 0 Y... (5 Replies)
Discussion started by: ipatah
5 Replies

6. Shell Programming and Scripting

Sort data As per first Column

hI I have file A NSU30504 5 6 G 6 NSU3050B T 7 9 J NSU30506 T I 8 9 NSU3050C H J K L Output: NSU3050B T 7 9 J NSU3050C H J K L NSU30504 5 6 G 6 NSU30506 T I 8 9Video tutorial on how to use code tags in The UNIX and Linux Forums. (13 Replies)
Discussion started by: pareshkp
13 Replies

7. UNIX for Dummies Questions & Answers

Sort on one column only

Hello, I am running on AIX.I have a question about sorting in UNIX. if my file is something like this: a c b d a b b c a a I want to sort on column 1 only. The following statement does not seem to work, it still considers the rest of the line in the sorting results: sort... (2 Replies)
Discussion started by: gio001
2 Replies

8. Shell Programming and Scripting

sort on second column only based on first column

I have an input file like this... AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Sunlight AAAlkalines Energizer AAAlkalines Energizer AAAlkalines Energizer AAASalines ... (7 Replies)
Discussion started by: malcomex999
7 Replies

9. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies

10. UNIX for Dummies Questions & Answers

Sort by the third column with +n command

my data.file: 1 2 3 3 2 1 3 5 8 4 9 11 3 3 30 I sort it by the 3rd column: sort -k3b,3 data.file I have : 3 2 1 4 9 11 1 2 3 3 3 30 3 5 8 How can I sort the 3rd column with the +n command? (2 Replies)
Discussion started by: bobo
2 Replies
Login or Register to Ask a Question