Computing.Net > Forums > Unix > Sort a file

Sort a file

Reply to Message Icon

Original Message
Name: Sara
Date: September 28, 2001 at 08:28:06 Pacific
Subject: Sort a file
Comment:

Right. I want to be able to sort lines in a file. However, these lines contain around 10 fields each seperated by a colon.

How can I sort these lines by field order?
ie. I want all the lines that contain similar to:
PATH=sara.txt,NAME=sara,TEST=thirty

to be sorted NAME=, TEST=, PATH= on every line.

Thanks,


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: September 29, 2001 at 06:29:33 Pacific
Subject: Sort a file
Reply: (edit)

Sara,
Don't know if you have colon delimiters or comma delimiters (use -t: or -t,).

sort -t, -k2,2 -k3,3 -k1,1 myfile > mynewfile

A big gotcha here is that if you specify only the sort key begin and not begin,end, it defaults to end-of-line. So, -k3,3 means field 3 thru field 3. And -k3 means field 3 thru end-of-line which will negate the effect of subsequent sort keys. Example:

sort -t: -k3 -k2 myfile

has k3 thru end-of-line as its first sort key, so the 2nd sort key will have no impact except for a series of lines where k3 thru end-of-line are identical.

James


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Sort a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge