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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting date field 1

Status
Not open for further replies.

nnikolay

Technical User
Sep 27, 2003
5
AU
I need to sort a file on the date field. My dates are in the second field and ":" delimited, with the mm/dd/yy format.
I have tried the following code but it sorts only on the month :
cat data | sort -t \: +1 -n > temp
So if I have original dates:
09/28/03
07/04/04
09/29/03
05/06/06
The output is:
05/06/06
07/04/04
09/28/03
09/29/03
Please help.
 
Try something like this:
Code:
sort -t: +1.6 -2 +1 data > temp


Hope This Help
PH.
 
Can someone explain the syntax of PHV's sort command? I know that the -t is used for the field separator, but I never really understood the + and - signs.

Thanks,

John
 
From
Code:
 man sort
+pos1 [ -pos2 ]
This notation restricts a sort key to one beginning at pos1 and ending at pos2. The characters at positions pos1 and pos2 are included in the sort key (provided that pos2 does not precede pos1). A missing -pos2 means the end of the line.
In this form of key field specification, fields are numbered in ascending order, starting from 0. The character position in a field can also be referenced, starting from 0 (for the first character).
All blanks in a sequence of blanks are considered to be part of the next field. For example, all blanks at the beginning of a line are considered to be part of the first field.
pos1 and pos2 each have the form: m[.n][flag]
A starting position specified by +m.n is interpreted to mean the (n+1)th character in the (m+1)th field. A missing .n means .0, indicating the first character of the (m+1)th field. flag is one of the modifiers b, d, f, i, n, or r. If the b flag is in effect, n is counted from the first non-blank in the (m+1)th field; +m.0b refers to the first non-blank character in the (m+1)th field.
A last position specified by -m.n is interpreted to mean the nth character (including separators) after the last character of the mth field. A missing .n means .0, indicating the last character of the mth field. If the b flag is in effect, n is counted from after the final leading blank in the (m+1)th field; -m.0b refers to the first non-blank in the (m+1)th field.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top