sort comma separated lines by specific columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sort comma separated lines by specific columns
# 1  
Old 12-05-2011
sort comma separated lines by specific columns

Hello,

I have a file which lines' words are comma separated:

Code:
aa, bb, cc, uu b, ee, ff
bb, cc, zz, ee, ss, kk
oo, bb, hh, uu a, xx, ww
tt, aa, dd, yy aa, gg

I want to sort first by second column and in case of tie by fourth column with sort command.

So the output would be:

Code:
tt, aa, dd, yy aa, gg           
oo, bb, hh, uu a, xx, ww
aa, bb, cc, uu b, ee, ff
bb, cc, zz, ee, ss, kk

Tried with:

Code:
sort -t, -1 +2 -3 +4 -5 file

But seems not to work.
Thank you
# 2  
Old 12-05-2011
Code:
sort -k2 -k4 infile

# 3  
Old 12-05-2011
It seems not to work:

Code:
~$ cat infile 
aa, bb, cc, uu b, ee, ff
bb, cc, zz, ee, ss, kk
oo, bb, hh, uu a, xx, ww
tt, aa, dd, yy aa, gg
~$ sort -k2 -k4 infile 
tt, aa, dd, yy aa, gg
aa, bb, cc, uu b, ee, ff
oo, bb, hh, uu a, xx, ww
bb, cc, zz, ee, ss, kk
~$

As you can see,
Code:
aa, bb, cc, uu b, ee, ff
oo, bb, hh, uu a, xx, ww

have the same "bb" k2 field, but different k4 field:
uu b
and
uu a

so as they are equals in field k2, the "uu a" line should be before "uu b" line as it's first in lexicografic order.

Last edited by radoulov; 12-05-2011 at 04:14 PM.. Reason: Additional code tags.
# 4  
Old 12-05-2011
Sorry:

Code:
sort -t, -k2,2 -k4,4 infile

# 5  
Old 12-05-2011
Ok, this one works fine.

Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Comma separated values to individual lines

My OS : RHEL 6.7 I have a text file with comma separated values like below $ cat testString.txt 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', . . . . I want these values to appear like below 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', .... (4 Replies)
Discussion started by: kraljic
4 Replies

4. UNIX for Dummies Questions & Answers

Cutting specific columns from lines

I am trying to remove columns 81-97 from a line that can be as long as 114 characters. Because a number of lines might not have under 80 characters, using the cut command following by paste could be a problem. While sed might work, is there some other utility that could do this more easily? ... (9 Replies)
Discussion started by: wbport
9 Replies

5. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

6. Shell Programming and Scripting

Make multiple lines into single quoted comma separated Linux

Hi, I want to change a file file1.txt: 1234 3456 2345 6789 3456 2333 4444 As, file2.txt in Linux: '1234','3456','2345','6789','3456','2333','4444' Could someone please help me. (Single liner sed, awk will be welcome!) (7 Replies)
Discussion started by: wiweq05
7 Replies

7. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

8. Shell Programming and Scripting

Need comma separated output

Hi, I am having the file with server names & its corresponding process, i need your help how to convert into comma separated output between server & app #cat apps.txt Server1 oracle was Server2 http webadmin Server3 tsm db2 My requirement is like below. Server1,oracle/was... (5 Replies)
Discussion started by: ksgnathan
5 Replies

9. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

10. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies
Login or Register to Ask a Question