Sort file using 2 columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort file using 2 columns
# 1  
Old 11-28-2009
Sort file using 2 columns

Hi, I am trying to sort a file first by the string column, then by the number column.

file:
Code:
xyz1    2       
xyzX    4        
xyz2    1       
xyz13   3       
xyz11   5  
xyz13  10     
xyz1    1        
xyz10   1        
xyz4    2

result should be
Code:
xyz1    1    
xyz1    2  
xyz2    1  
xyz4    2 
xyz10   1  
xyz11   5    
xyz13   3  
xyz13   10 
xyzX    4

I have tried many sort commands and failed. Please help!

---------- Post updated at 02:45 PM ---------- Previous update was at 12:32 AM ----------

please help!

Last edited by fargo; 11-28-2009 at 03:44 PM..
# 2  
Old 11-28-2009
try this:
Code:
> awk 'BEGIN {OFS="|"} {print $1,$2}' filename | sort -n | awk -F'|' 'BEGIN {OFS=" "}{print $1,$2}' > newfile

# 3  
Old 11-28-2009
Thanks. I think I have figured it out. The file is very large, so I need to use the unix sort.(not sure about the awk thing) I used sort -t ' ' -k
# 4  
Old 11-29-2009
You have to specify a numerical sort for the second column otherwise it will get sorted alphabetically. This should do it:
Code:
sort -k1,1 -k2,2n infile

With my sort you have to use a comma in the key field specification or it does not work right. Also, if you use -t' ' it seems to use single space separation instead of multispace separation and also produce incorrect result. Without -t it produces:
Code:
xyz1    1
xyz1    2
xyz10   1
xyz11   5
xyz13   3
xyz13   10
xyz2    1
xyz4    2
xyzX    4

But you required a numerical sort on the second part of the first key. For that the first key needs to be split into an alphabetical part and a numerical part during the sort phase. A numerical sort places X at the top though, not at the bottom.
Code:
sed 's/\(...\)/\1 /' infile|sort -k1,1 -k2,2n -k3,3n|sed 's/ //'

produces:
Code:
xyzX    4
xyz1    1
xyz1    2
xyz2    1
xyz4    2
xyz10   1
xyz11   5
xyz13   3
xyz13   10


Last edited by Scrutinizer; 11-29-2009 at 04:47 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seperated by columns, merge in a file, sort them on common column

Hi All, I have 4 files in below format. I took them as an example. File 1: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting character H to the initial of all line like HCTOT. CTOT 456787897 Low fever CTOR 556712345 High fever... (2 Replies)
Discussion started by: Mannu2525
2 Replies

2. Shell Programming and Scripting

How to sort a text file if certain columns are blank?

Dear all, I am trying to sort a text file based on column 3, 10, 11 and 12. But certain column are blank for some lines. Column 3 has to be in ascending order after sorting. Part of my input file is as follows: CN727990 1 A01 4703 5083 73.28 - A_scaffold000011 4365605 4365985 73.28 +... (10 Replies)
Discussion started by: huiyee1
10 Replies

3. Web Development

Sort 3 or more columns in a HTML file

Hi Friends, I have a HTMl file with 10 columns. I found a script online that can sort any single column in a HTML file. But, I would like to sort on multiple columns at once. Could you please show some pointers? Thanks (6 Replies)
Discussion started by: jacobs.smith
6 Replies

4. Shell Programming and Scripting

sort second columns in file.

File will have two columns key column and second column which is pipe separated and that need to be sorted. Below is input file. 1, D|B|A|C 2, C|A|B 3, E|A|F|G|H|D|B|C 4, A|B|D|C|F Output should be 1, A|B|C|D 2, A|B|C 3, A|B|C|D|E|F|G|H 4, A|B|D|C|F (11 Replies)
Discussion started by: girish119d
11 Replies

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

6. Shell Programming and Scripting

How to sort columns in excel(csv) file

i want sort columns with headers based on another file headers file1 eg: i'm having an empty file with only coumn names like lastname firstname title expirydate stlcno status etc... another file with same column names and some other as well but in different order... file2 eg:firstname... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies

7. Shell Programming and Scripting

sort columns by field

hello, i have a table contain many columns delimited by blank. i want to sort this table by the 2 columns and 3 one and i want to keep the first line inchanged? how can i do using the sort command? thanks table like : field1 field2 field3 field4 x y z b t h r n .. (4 Replies)
Discussion started by: kamel.seg
4 Replies

8. Shell Programming and Scripting

add columns from file to another and sort

hi, i have probleme to extract the columns info from file to another one; FILE A look like : x,inof1 y,inof1 z,info2 t,inof3 and FILE B like x t w d z i want to add correpondant columns (info) to each line of FILE B and sort this file by this columns. (12 Replies)
Discussion started by: kamel.seg
12 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
Login or Register to Ask a Question