Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting : delimited File with unknown number of fields

Status
Not open for further replies.

gregc73

Technical User
Dec 16, 2003
5
US
I am trying to sort a file that could have anywhere from 1-35 different : delimited fields in it. I want it to sort each field independently (not treat the entire line as a single string). However, it seems that the sort command doesn't recognize the -t ':' command unless I use it in conjuction with the -k command. Since I don't know how many fields the file might have, using the -k command isn't a simple excercice.

The file would look something like this:

1:2:3:
1:1:2:
1:1::
1::1

and the command I am using is

sort -t ':' filename

The most notable thing I am seeing is that it seems to ignore the null values when two delimiters are next to each other. Normally those values would come first in the sort order, but in this case, they are coming last.
 

Try something like this:

Code:
sort -t':' -n -k1,9 data.txt >newdata.txt

PS: 9 would be the max number of "fields". [3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
The max number of fields that will be in the file will be 23, so what seems to work is this:

sort -t ':' -k1,1 -k2,2 -k3,3 -k4,4 -k5,5 -k6,6 -k7,7 -k8,8 -k9,9 -k10,10 -k11,11 -k12,12 -k13,13 -k14,14 -k15,15 -k16,16 -k17,17 -k18,18 -k19,19 -k20,20 -k21,21 -k22,22 -k23,23 -o <newfile> <oldfile>

While it is a little verbose, it seems to work, so I guess I'll go with it. If there are only 5 fields in the file, I'm assuming 6-23 will just be ignored.
 

Did you try -k1,23?:

Code:
sort -t':' -k1,23 <newfile> -o <oldfile>
[noevil]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
I did try that but that seems to treat the entire line as a continuous string.

In other words, if the line was 1:2:3, it doesn't first sort all the 1's together, it instead sorts all the 123's together.
 
And this ?
sort -t':' [!]-n[/!] -k1,23 <newfile> -o <oldfile>

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Yes, I had also suggested the -n option in case all fields are numeric. Did you try it?


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top