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!

Ignoring white space for a sort. 2

Status
Not open for further replies.

btinder

Programmer
Mar 12, 2004
27
US
Hi all,

I've got a file that has lines likes this:

2 123456789 0501 A B

The primary key to sort on is the 123456789 and the secondary sort key is the 0501 (year/month).

My current sort command is this:
sort +1n -2 +2n -3 ${file}.stripped > ${file}.sorted

A problem exists though, when you encounter a record that has a space in the part. It does the secondary sort incorrectly. Below is an example of what the sort does on 2 identical records but the date is different in both.

2 123456789 0501 A B
2 123456789 0401 A B

Note how the 0401 part should be above the 0501.

Can someone help? Thanks so much.
 

Field separator is SPACE, sort is behaving as it should.

Correct the records with incorrect keys.



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA is right. Is it possible to change your field separator to something other than SPACE?

Also, is your data fixed? Are you always sorting by columns 3-14 and 16-19?



 
Yes, the data is fixed. No, we can't modify the data, since it comes from an extract of a database.
 
IF the data is truely "fixed" (meaning for every record the same field starts at the same column), then what olded I assumming was leaning towards was the following:


sort -k 1.3,1.18 -k 1.19,22 .......

anotherwords, pick-off the starting and ending column positions for each field.

 
That's exactly what I was leaning towards, but I think you have to use a character that's not being used for a field separator so the whole line is 'field 1':

sort -t~ -k 1.3,1.17 -k 1.19,1.22 d1.file

assumes the tilde, ~, isn't used in the data.

 
I disagree ...

You're treating the whole line as 1 field, thus you don't need to explicitly define a field separator (i.e. - space, tab, "," , etc.).

Defining one with the -t option in this case/example has no effect.
 
I think it worked! Thanks much for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top