Hi,
I am unable to sort data on the first field
$cat t
Jim,212121,Seattle
Bill,404404,Seattle
Steve,246810,Nevada
Scott,212277,LosAngeles
Jim,212121,Ohio
sort -t"," -k1,2 t
Bill,404404,Seattle
Jim,212121,Ohio
Jim,212121,Seattle
Scott,212277,LosAngeles
Steve,246810,Nevada
I expect "Jim,212121,Seattle" to be on top of "Jim,212121,Ohio"
It again has the same problem. I want "Jim,212121,Seattle" should be coming before "Jim,212121,Ohio" as that comes first in the order in the file.
I just want to sort on 1 st field
Scott
June 18, 2011, 8:34am
4
Maybe you need a "stable" sort:
$ sort -s -t"," -k1,2 t
Bill,404404,Seattle
Jim,212121,Seattle
Jim,212121,Ohio
Scott,212277,LosAngeles
Steve,246810,Nevada
Doesnt work for me on Solaris.
Tried both the variants
/usr/bin/sort & /usr/xpg4/bin/sort
$/usr/bin/sort -s -t"," -k1,2 t
/usr/bin/sort: illegal option -- s
usage: sort [-cmu] [-o output] [-T directory] [-S mem] [-z recsz]
[-dfiMnr] [-b] [-t char] [-k keydef] [+pos1 [-pos2]] files...
$/usr/xpg4/bin/sort -s -t"," -k1,2 t
/usr/xpg4/bin/sort: illegal option -- s
usage: sort [-cmu] [-o output] [-T directory] [-S mem] [-z recsz]
[-dfiMnr] [-b] [-t char] [-k keydef] [+pos1 [-pos2]] files..
but thats the o/p i want
---------- Post updated at 06:29 PM ---------- Previous update was at 06:10 PM ----------
Solaris doesnt seem to have an option for Stable sort.
The man pages doesnt have any mention of that
maybe you can try with sed and sort..
# for i in $(sed -e 's/\([^ ]*\),[^ ]*,[^ ]*/\1/' t| sort -u| sed -e ':a' -e '$!N;s/\n/ /' -e 'ta'); do sed -n "/$i/p" t; done
Bill,404404,Seattle
Jim,212121,Seattle
Jim,212121,Ohio
Scott,212277,LosAngeles
Steve,246810,Nevada
regards
ygemici
yazu
June 18, 2011, 11:07am
7
@ygemici Wow! Really great!
But maybe Perl is a little simpler:
% cat testfile| perl -F"," -alne 'push @V, [$F[0], $_];
END { print $_->[1] for sort {$a->[0] cmp $b->[0]} @V}'
Bill,404404,Seattle
Jim,212121,Seattle
Jim,212121,Ohio
Scott,212277,LosAngeles
Steve,246810,Nevada
yazu:
@ygemici Wow! Really great!
But maybe Perl is a little simpler:
% cat testfile| perl -F"," -alne 'push @V, [$F[0], $_];
END { print $_->[1] for sort {$a->[0] cmp $b->[0]} @V}'
Bill,404404,Seattle
Jim,212121,Seattle
Jim,212121,Ohio
Scott,212277,LosAngeles
Steve,246810,Nevada
you can use also perl of course and you dont need `cat`.
regards
ygemici