Hi Everyone,

I have the data like below in CSV file

Column1,Column2
A,3
A A,4
A L,5
AIM,6

If i sort the above mentioned date in excel, here is the sorted output:

Column1,Column2
A,3
A A,4
A L,5
AIM,6

But if i tried to sort above data by using Sort in shell script, this is how i got:
Column1,Column2
A,3
A A,4
AIM,6
A L,5

If we notice the output of shell sort command, it seems like omitting white space while sorting.

My requirement is that the data have to be sorted out the way the excel does.

Can you please let me know how to do this shell script?

Thanks in advance

Gopi

Your sort is doing a sort by native byte values (space is ascii 32, which is less than the ascii codes for the other characters).

If you can, use the gnu sort (this is the default for linux). It sorts the same way as excel does because it uses your locale setting to determine the sort order.

Yes, sort with Gnu does give different results. Also, if you need to, try using the -t option if you need to specifically identify the delimiter that your cells in the csv file are being separated by.
It could be that the default IFS (one or multiple blank spaces) doesn't agree with that.

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.