3

This is more of a doubt than a question.

So I have an input file like this:

$ cat test class||sw sw-explr bot|results|id,23,0a522b36-556f-4116-b485-adcf132b6cad,20130325,/html/body/div/div[3]/div[2]/div[2]/div[3]/div/div/div/div/div/div[2]/div/div/ul/li[4]/div/img class||sw sw-explr bot|results|id,40,30cefa2c-6ebf-485e-b49c-3a612fe3fd73,20130323,/html/body/div/div[3]/div[2]/div[3]/div[3]/div/div/div/div/div[3]/div/div/ul/li[8]/div/img class||sw sw-explr bot|results|id,3,72805487-72c3-4173-947f-e5abed6ea1e4,20130324,/html/body/div/div[3]/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div/div/div[2]/ul/li[20]/div/img 

Kind of defining the element in an html page. The comma separated 5 columns can be considered.

I want to sort this file with respect to the second column, i.e. columns having 23,40,3.

I am not sure why unix sort isn't working.

These are the queries I tried, surprisingly none gave me desired result.

cat test | sort -nt',' -k2 cat test | sort -n -t, -k2 cat test | sort -n -t$',' -k2 cat test | sort -t"," -k2 cat test | sort -n -k2 

Is there something about sort that I don't know?

This didn't cause me a problem as I separated the columns, sorted, then joined again. But why did not sort work??

NB:- If I remove $3 of this file and then sort, it works fine!

2 Answers 2

4

this line should work for you:

sort -t, -n -k2,2 test 
  • you don't need cat test|sort, just sort file
  • the default END POS of -k is the end of line. so if you sort -k2 it means sort from the 2nd field till the end of line. In fact you need sort by exact the 2nd field. And this also explains why your sort worked if you removed 3rd col.

if test with your example:

kent$ sort -t, -n -k2,2 file class||sw sw-explr bot|results|id,3,72805487-72c3-4173-947f-e5abed6ea1e4,20130324,/html/body/div/div[3]/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div/div/div[2]/ul/li[20]/div/img class||sw sw-explr bot|results|id,23,0a522b36-556f-4116-b485-adcf132b6cad,20130325,/html/body/div/div[3]/div[2]/div[2]/div[3]/div/div/div/div/div/div[2]/div/div/ul/li[4]/div/img class||sw sw-explr bot|results|id,40,30cefa2c-6ebf-485e-b49c-3a612fe3fd73,20130323,/html/body/div/div[3]/div[2]/div[3]/div[3]/div/div/div/div/div[3]/div/div/ul/li[8]/div/img 
Sign up to request clarification or add additional context in comments.

2 Comments

Hey can you please explain why other sort commands didnt work even when they work elsewhere??
@rohitvk I added an explanation. check if it helps.
3

Here comes a working solution:

cat test.file | sort -t, -k2n,2 

Explanation:

-t, # Set field separator to ',' -k2n,2 # sort by the second column, numerical 

5 Comments

Shouldn't that be -k2n,2 to sort only on the second field instead of from the second field to the end of the line?
@Kent I've tested it with the example data, but I think Frederic is right, thanks! :) (althought it doesn't matter in this case)
Yes, as I said, it doesn't matter in this case
thanks now it does. Your previous version, b4 editing I tried. Not this one. Now it works! And I also understood how. Thanks @FrédéricHamidi
This first one should have worked for your special data as well.. But there would be unwanted side effects if, for example two rows would contain 40,1... and 40,2... they would get sorted by the text after the , and this isn't expected. That's why k2n,2 instead of k2n :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.