using sort command

Rei Shinozuka shino at panix.com
Mon Apr 26 16:16:40 UTC 2010


On 04/26/2010 11:44 AM, R Kimber wrote:
> I'm trying to sort a text file of the form:
> john smith, something, something
>
> Basically, I want to sort on the surname and then on the first name, so
> that all the Smiths (e.g.) end up in order of their first name.
>
> The man page doesn't seem to explain this. Googling suggests that
>
> sort -k2 -k1<filename>
>
> ought to work, but it just seems to sort on the surname.
>
> I'd be grateful for pointers on how to do this.
> - Richard.
>    
i think you want:
sort -k2,2 -k1,1 <filename>

as shown below (comma used as delimiter for readability)

  $ cat names.txt
Katherine,Hepburn,1907
John,Wayne,1907
Efrem,Zimbalist,1918
William,Holden,1918
Walter,Brennan,1894
Audrey,Hepburn,1929

  $ sort -t, -k1 names.txt
Audrey,Hepburn,1929
Efrem,Zimbalist,1918
John,Wayne,1907
Katherine,Hepburn,1907
Walter,Brennan,1894
William,Holden,1918

  $ sort -t, -k2 names.txt
Walter,Brennan,1894
Katherine,Hepburn,1907
Audrey,Hepburn,1929
William,Holden,1918
John,Wayne,1907
Efrem,Zimbalist,1918

  $ sort -t, -k2 -k1 names.txt
Walter,Brennan,1894
Katherine,Hepburn,1907
Audrey,Hepburn,1929
William,Holden,1918
John,Wayne,1907
Efrem,Zimbalist,1918

  $ sort -t, -k2,2 -k1,1 names.txt
Walter,Brennan,1894
Audrey,Hepburn,1929
Katherine,Hepburn,1907
William,Holden,1918
John,Wayne,1907
Efrem,Zimbalist,1918

-rei





More information about the ubuntu-users mailing list