Home > OS >  how to check if a file is sorted on nth column in unix?
how to check if a file is sorted on nth column in unix?

Time:12-09

lets say that I have a file as below:(comma separated)

cat test.csv
Rohit,India
Rahul,India
Surya Kumar,India
Shreyas Iyer,India
Ravindra Jadeja India
Rishabh Pant India
zzabc,abc

Now I want to check if the above file is sorted on 02nd column. I tried the command sort -ct"," -k2,2 test.csv I'm expecting it to say disorder in last line, but it is giving me disorder in 02nd line. Could anybody tell me what is wrong here? and how to get the expected output?

CodePudding user response:

You need to disable the "last-resort comparison" by using a stable sort.

# order is changed based on the first column also
savuso:so$ sort -t"," -k2,2 test.csv 
zzabc,abc
Rahul,India
Ravindra Jadeja,India
Rishabh Pant,India
Rohit,India
Shreyas Iyer,India
Surya Kumar,India

#first column is ignored.
savuso:so$ sort -st"," -k2,2 test.csv 
zzabc,abc
Rohit,India
Rahul,India
Surya Kumar,India
Shreyas Iyer,India
Ravindra Jadeja,India
Rishabh Pant,India

savuso:so$ sort -ct"," -k2,2 test.csv
sort: test.csv:2: disorder: Rahul,India

savuso:so$ sort -cst"," -k2,2 test.csv
sort: test.csv:7: disorder: zzabc,abc
savuso:so$

CodePudding user response:

The sort is not guaranteed to be stable. But some implementations of sort support an option which will force that. Try adding -s:

sort -sc -t, -k2,2 test.csv

but note that I would expect the first out of order line to be Ravindra Jadeja India, since the 2nd field of that line is the empty string which should sort before "India".

  • Related