Solaris + sorting file according to date and time by sort command

1

I have along list that created in /var/tmp/file.txt from some script (in Solaris machine) the following list have 4 fields

please advice how to sort the list according to the following TIMESTAMP ( by sort command or other solaris command as awk or sed ..... )

for example the date & time 15-10-2009 08:29:18 should be before 15-10-2009 08:29:10 ... etc

example of file.txt ( not sorted file )

  PHONE_NUMBER         TIMESTAMP                   ID  TYPE
  -------------------- -------------------        ---- -------------- 
  972544111222         15-10-2009 08:29:18         20  sharp_gx10
  33633333333          24-09-2009 16:17:45         20  other_mm_phone
  841990000043         08-10-2009 09:04:38         60  other_mm_phone
  972541230001         08-10-2009 14:23:48         20  other_mm_phone

yael

Posted 2012-04-17T07:46:13.000

Reputation: 91

1Well, the timestamp format in itself is annoying :-) . If it were the internationally recognized "YYYY-MM-DD HH:mm:ss", then a simple numeric (alpha, even) sort on those combined fields would be trivial. Still possible to fix, just annoying. – Daniel Andersson – 2012-04-17T08:00:05.227

s/internationally recognized/ISO 8601 / RFC 3339/ – Eroen – 2012-04-17T09:06:52.313

Answers

0

As I said in a comment, the timestamp format makes my approach seem convoluted, but it is mostly mechanic repetition.

awk 'NR>2 {print $1"-"$2"-"$3"-"$4"-"$5}' file.txt |\
awk -F'-' '{print $4"-"$3"-"$2"-"$5"-"$1"-"$6"-"$7}' |\
sort |\
awk -F'-' '{print $5"\t"$1"-"$2"-"$3" "$4"\t"$6"\t"$7}'
  1. Skip the first two lines (just a header) and transform the remaining lines to use a single delimiter (dash in this case, make sure it is not allowed to appear in the last field) (originally it alternates between whitespace and dash, because of the date format),
  2. split fields with dash and move the date is to the beginning of the line (for ease of use with sort) and print it in a descending format,
  3. sort and
  4. print the information back out in a format similar to the input (exchange $1 and $3 to get the old date format).

Add the header lines back afterwards if they are wanted.


EDIT: I saw that you wanted a reverse sort. Just change sort to sort -r.

Daniel Andersson

Posted 2012-04-17T07:46:13.000

Reputation: 20 465