Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
Well, 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 Apr 17 '12 at 8:00
s/internationally recognized/ISO 8601 \/ RFC 3339/ – Eroen Apr 17 '12 at 9:06
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.