Custom sort on a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Custom sort on a column
# 1  
Old 12-10-2014
Custom sort on a column

Hello all,

How do I achieve this? I have A, B and A/B in different variables in a file in col2.
I want them to sort in such a way, that the variables appear together, and within a variable, the data is sorted in the order A,B and then A/B. If I sort on the second column, the order becomes A, A/B, B but I want A,B,A/B.

IN
Code:
Variable1 A/B 1
Variable2 A    2
Variable3 B    3
Varibale2 B    4
Variable2 A/B 5
Variable2 A/B 6
Varibale1 A/B 7
Variable1 B    8
Variable1 A    9
Variable3 A    10
Variable3 A/B 11

OUT
Code:
Variable1 A    9
Variable1 B    8
Variable1 A/B 1
Varibale1 A/B 7
Variable2 A    2
Varibale2 B    4
Variable2 A/B 5
Variable2 A/B 6
Variable3 A    10
Variable3 B    3
Variable3 A/B 11

# 2  
Old 12-10-2014
Try:
Code:
sort -k1,1 -k2.3,2.3 -k2.1,2.1 file




--
Note, there are letters reversed
Code:
..
Varibale2 B    4
Variable2 A/B 5
Variable2 A/B 6
Varibale1 A/B 7
..

in your sample input file
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-14-2014
This doesn't seem to work for a comma delimited file if I use sort -t, option. Any easy fix for this?
# 4  
Old 12-14-2014
It should, could you provide a sample input file?
# 5  
Old 12-14-2014
Here is an example

Code:
 
 cat > tmp
XTurb7670,B,AA,Filo060E-02052
XTurb7670,A/B,AA,Filo067E-02068
XTurb7670,A/B,AA,Filo067E-02070
XTurb7670,A/B,AA,Filo067E-02071
XTurb7670,A/B,AA,Filo067E-02073
XTurb7670,A/B,AA,Filo067E-02075
XTurb7670,A/B,AA,Filo067E-02076
XTurb7670,A/B,AA,Filo067E-02083
XTurb7670,A/B,AA,Filo067E-02084
XTurb7670,A/B,AA,Filo067E-02086
XTurb7670,A,GG,Filo067E-02065
 $ sort -t, -k1,1 -k2.3,2.3 -k2.1,2.1 tmp
XTurb7670,B,AA,Filo060E-02052
XTurb7670,A/B,AA,Filo067E-02068
XTurb7670,A/B,AA,Filo067E-02070
XTurb7670,A/B,AA,Filo067E-02071
XTurb7670,A/B,AA,Filo067E-02073
XTurb7670,A/B,AA,Filo067E-02075
XTurb7670,A/B,AA,Filo067E-02076
XTurb7670,A/B,AA,Filo067E-02083
XTurb7670,A/B,AA,Filo067E-02084
XTurb7670,A/B,AA,Filo067E-02086
XTurb7670,A,GG,Filo067E-02065

# 6  
Old 12-14-2014
Seems not to be due to the delimiter but due to an inappropriate key. -k2.3 points to nowhere for first and last line.
# 7  
Old 12-14-2014
This will do what I think you want for your sample input on some systems (including OS X):
Code:
sort -t, -k1,1 -k2.3,2 -k2.1,2.1 tmp

producing:
Code:
XTurb7670,A,GG,Filo067E-02065
XTurb7670,B,AA,Filo060E-02052
XTurb7670,A/B,AA,Filo067E-02068
XTurb7670,A/B,AA,Filo067E-02070
XTurb7670,A/B,AA,Filo067E-02071
XTurb7670,A/B,AA,Filo067E-02073
XTurb7670,A/B,AA,Filo067E-02075
XTurb7670,A/B,AA,Filo067E-02076
XTurb7670,A/B,AA,Filo067E-02083
XTurb7670,A/B,AA,Filo067E-02084
XTurb7670,A/B,AA,Filo067E-02086

This User Gave Thanks to Don Cragun 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

Sort file data according to a custom list of string

I have a string of pre defined ip address list which will always remain constant their order will never change like in below sample: iplist=8.8.5.19,9.7.5.14,12.9.9.23,8.8.8.14,144.1.113 In the above example i m considering only 5 ips but there could be many more. Now i have a file which... (15 Replies)
Discussion started by: mohtashims
15 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 Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

4. Shell Programming and Scripting

Sort and Split file with header and custom name

Hi, I am using SUN SOLARIS (SunOS sun4v sparc SUNW, T5240). I have a huge data file with header and trailer. This file gets used into an ETL process. ETL skips the header record (which is the first record of the file) and loads the rest of the record. The file can be delimited (comma,... (5 Replies)
Discussion started by: Saanvi1
5 Replies

5. Shell Programming and Scripting

Find and sort by first column value

Hi, I have two text files file 1 with N lines AAAAA 2.092290E-12 BBBBB 1.727740E-07 CCCCC 9.608710E-17 DDDDD 0.000000E+00 EEEEE 0.000000E+00 FFFFF 0.000000E+00 GGGGG 0.000000E+00 HHHHH 0.000000E+00 IIIII 3.300320E-04 ... The text in the first column is unique for each row and... (4 Replies)
Discussion started by: f_o_555
4 Replies

6. Shell Programming and Scripting

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 sort -u -t'|' -k4,4r file1 > file2 time|tourit|nofdays|rbcid|blank|type|value|nill|valuedesc|name... (6 Replies)
Discussion started by: samrat dutta
6 Replies

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question