Visit Our UNIX and Linux User Community


sort on multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort on multiple columns
# 1  
Old 07-05-2012
sort on multiple columns

hi all,
i have a file , having few columns. i wanted to sort it based on 2nd column and then based on 1st column. But i have some problem in first column.
first column have characters and numbers, but problem is number of characters are not same in all rows. Few rows have 13 characters and then 14th,15th number , some rows have 14 characters and 15th number. and so i am getting below output which i dont want (i tried to search this type of problem on this portal, but i dint get) :-
below are 1st and 2nd column
HTML Code:
ocsrplabcpapp1 /ploam1/fs_users 
ocsrbabcpapp1 /olclocal/fs_users 
[B]ocsrplabcpapp1 /plbnsrpl01/fs_users 
ocsrbabcpapp10 /olclocal/fs_users
ocsrbabcpapp11 /olclocal/fs_users
ocsrbabcpapp2 /olclocal/fs_users [/B]
ocsrbabcpapp3 /olclocal/fs_users
ocsavmabcpapp1 /olclocal/fs_users 
ocsavmabcpapp2 /olclocal/fs_users  
ocsrplabcpapp2 /olclocal/fs_users
ocsfrabcpapp1 /plfuh01/fs_users 
ocsrplabcpapp2 /plrpb02/fs_users 
output i want as below :-
HTML Code:
ocsavmabcpapp1 /olclocal/fs_users 
ocsavmabcpapp2 /olclocal/fs_users 
[B]ocsrbabcpapp1 /olclocal/fs_users 
ocsrbabcpapp2 /olclocal/fs_users 
ocsrbabcpapp3 /olclocal/fs_users 
ocsrbabcpapp10 /olclocal/fs_users
ocsrbabcpapp11 /olclocal/fs_users[/B]
ocsrplabcpapp2 /olclocal/fs_users
ocsfrabcpapp1 /plfuh01/fs_users 
ocsrplabcpapp1 /plbnsrpl01/fs_users 
ocsrplabcpapp2 /plrpb02/fs_users
# 2  
Old 07-05-2012
Try like... untested..
Code:
sort -k1,1 test.txt

# 3  
Old 07-05-2012
no , it will not work, tried already
# 4  
Old 07-05-2012
Could you be more precise wrt the type of sorting you're aiming for the first column? Do you want a simple sort like:

Code:
$ cut -d" " -f1 sort_your_example.txt | sort
ocsrplabcpapp1
ocsavmabcpapp1
ocsavmabcpapp2
ocsfrabcpapp1
ocsrbabcpapp1
ocsrbabcpapp10
ocsrbabcpapp11
ocsrbabcpapp2
ocsrbabcpapp3
ocsrplabcpapp1
ocsrplabcpapp2
ocsrplabcpapp2



Or are you aiming for that type of sort wrt the 2 columns? :
Code:
$ sort -k1 sort.txt | sort -k2
ocsrbabcpapp10 /olclocal/fs_users
ocsrbabcpapp11 /olclocal/fs_users
ocsrbabcpapp3 /olclocal/fs_users
ocsrplabcpapp2 /olclocal/fs_users
ocsavmabcpapp1 /olclocal/fs_users 
ocsrbabcpapp1 /olclocal/fs_users 
ocsavmabcpapp2 /olclocal/fs_users  
ocsrbabcpapp2 /olclocal/fs_users

Code:
[B]ocsrplabcpapp1 /plbnsrpl01/fs_users 
ocsfrabcpapp1 /plfuh01/fs_users 
ocsrplabcpapp1 /ploam1/fs_users 
ocsrplabcpapp2 /plrpb02/fs_users

IF you're aiming for a mix of both, I suggest you cut, sort and then simply join the two...

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ls -ltr Sort multiple columns

Hi All, I have one requirement, where I need to have output of ls -l command sorted on 1) first on filename 2) last modified time ( descending ) - latest change first. I am not able to figure out how to do it.. Also I dont have a way to change Date display for ls -ltr command.. I am... (1 Reply)
Discussion started by: freakabhi
1 Replies

2. UNIX for Dummies Questions & Answers

UNIX Sort on a field that spans multiple columns

New to unix. I need to sort the records of a file by a control number field. That field is in POS 16 through 28. How do I do that? There are no delimiters, or spaces to separate fields. See example below. Each line is a record. REC1CCYYMMDD0018888888888888ABCDE... (1 Reply)
Discussion started by: jclanc8
1 Replies

3. Shell Programming and Scripting

Sort based on Multiple Columns in UNIX

Hi, I would like to sort a list in different ways: 1> Unique based on Field 1 with highest Field 4 For Instance Input: 1678923450;11112222333344;11-1x;2_File.xml 1678923450;11112222333344;11-1x;5_File.xml 1234567890;11113333222244;11-1x;3_File.xml Output: ... (7 Replies)
Discussion started by: DevendraG
7 Replies

4. Shell Programming and Scripting

Sort and join multiple columns using awk

Is it possible to join all the values after sorting them based on 1st column key and replace empty rows with 0 like below ? input a1 0 a1 1 a1 1 a3 1 b2 1 a2 1 a4 1 a2 1 a4 1 c4 1 a3 1 d1 1 a3 1 b1 1 d1 1 a4 1 c4 1 b2 1 b1 1 b2 1 c4 1 d1 1 output... (8 Replies)
Discussion started by: quincyjones
8 Replies

5. Shell Programming and Scripting

sort on multiple columns

Howdy! Need to sort a large .txt file containing the following, using sort. First based on the 1st column, and then on the 2nd column: Group01.01 1000500 31 0.913 -1.522974494 Group01.01 1001500 16 0.684 -0.967496041 Group01.01 36500 19 0.476 na Group01.02 365500 15 0.400 na... (1 Reply)
Discussion started by: sramirez
1 Replies

6. UNIX for Dummies Questions & Answers

Sort by columns

Hello, I have a text file that looks like this and I need a bash script to: 12:48:32 PM 002* OUT 000418 01:10:34 PM 002* ONL 000418 01:49:17 PM 001* OUT 000364 01:52:09 PM 001* ONL 000364 ... The fields are: 12-hour format time, some number, state (online, offline) and another... (2 Replies)
Discussion started by: Ravendark
2 Replies

7. Shell Programming and Scripting

sort by based on multiple columns

Hi, Is there any way to sort a file in cshell by sort command, sorting it by multiple fields, like to sort it first by the second column and then by the first column. Thanks forhead (1 Reply)
Discussion started by: Takeeshe
1 Replies

8. Shell Programming and Scripting

sort by multiple columns and reformat...

hello.. I have big file and so far I was able to shink it and make smaller with certains values that I need.. vendor1|2000|1 vendor2|1000|1 vendor2|5000|1 vendor2|500|2 vendor3|1000|2 vendor3|500|3 vendor4|500|3 Vendor5|500|1 vendor6|500|3 Vendor7|1000|1 Vendor8|1000|774... (3 Replies)
Discussion started by: abdulaziz
3 Replies

9. UNIX for Dummies Questions & Answers

Help needed to sort multiple columns in one file

Hi, I would like to know given that I have 3 columns. Let say I have first 3 columns to do operation and these operation output is printed out each line by line using AWK and associative array.Currently in the output file, I do a sort by -r for the operation output. The problem comes to... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. UNIX for Dummies Questions & Answers

Sort by Columns

Hello, I am new in UNIX I am looking for a instrction to sort a file by columns 6,25 and 41 this is what I tried but not getting the correct result: sort -t= -k1.6,1.25,1.41 to_sort.txt > sorted.txt I used -t= just to get the whole line as one field. INVS80993596SUM994338602XX... (1 Reply)
Discussion started by: murbina
1 Replies

Featured Tech Videos