1

I want to Sort the data in a file considering the date column (last column in the sample data). I have tried some commands but didn't get succeeded.

Sample data:

Beendet|48149|1550804401|STR|Fri Feb 22  4:00:45 2019
Beendet|940001|1550790961|CBST|Fri Feb 22  0:16:12 2019
Beendet|658521|1550817001|CANS|Fri Feb 22  7:30:10 2019
Beendet|48150|1550775601|EXP|Thu Feb 21 20:00:12 2019
Beendet|7|1550779201|MAS|Thu Feb 21 21:00:23 2019
Beendet|9072|1550777401|AMS0|Thu Feb 21 20:52:17 2019
Beendet|9220|1550804401|AMS1|Fri Feb 22  4:00:09 2019
Beendet|670|1550811601|MOR|Fri Feb 22  6:00:07 2019
Beendet|73|1550790061|HIST|Fri Feb 22  2:00:30 2019
Beendet|122|1550804821|HIST1|Fri Feb 22  4:07:08 2019

These are my tries:

sort -r -k5.12 -k5.9 completefile.txt (sorting using the date and time field lengths)
sort -n -k5,5 completefile.txt 
sort -k5n,5 completefile.txt

All are giving the same result.

2

Awk script: convert the last field to timestamp and compare.

#!/usr/bin/awk -f

BEGIN {
    FS = "|"
}

{
    data[NR] = $0
    cmd = "date \"+%s\" --date " "\"" $5 "\""
    cmd|getline
    stamp2idx[$0] = NR
}

END {
    asorti(stamp2idx, sorted)
    for (n in sorted) {
        print data[stamp2idx[sorted[n]]]
    }
}

output

➤ ./solution.awk data                                                       20:39:09
Beendet|48150|1550775601|EXP|Thu Feb 21 20:00:12 2019
Beendet|9072|1550777401|AMS0|Thu Feb 21 20:52:17 2019
Beendet|7|1550779201|MAS|Thu Feb 21 21:00:23 2019
Beendet|940001|1550790961|CBST|Fri Feb 22  0:16:12 2019
Beendet|73|1550790061|HIST|Fri Feb 22  2:00:30 2019
Beendet|9220|1550804401|AMS1|Fri Feb 22  4:00:09 2019
Beendet|48149|1550804401|STR|Fri Feb 22  4:00:45 2019
Beendet|122|1550804821|HIST1|Fri Feb 22  4:07:08 2019
Beendet|670|1550811601|MOR|Fri Feb 22  6:00:07 2019
Beendet|658521|1550817001|CANS|Fri Feb 22  7:30:10 2019
  • Thank you...I will test this script in my environment. – Ravi Feb 22 at 13:13
1

If I understand correctly to get the intended order, you should specify the sort criteria for each field:

sort -k5n -k2M -k3n -k4V file.txt

Where

  • -k5n col 5 (year) in numerical order
  • -k2M col 2 in "month" order
  • -k3n col 3 (day) in numerical order
  • -k4V col 4 (time) in "Version" order

If you can change the log process, consider using date -Is (2019-02-22T12:20:28+00:00), and sorting with

sort -t '|' -k5   file.txt
  • Thank you, I will try and provide you an update. – Ravi Feb 22 at 12:36
  • One thing I have tried is : consider the command: cut -f5 -d '|' completefile.txt | sort -r -k1.8,1.18 which sorts perfectly only if I consider the date column. How I can use this in the sample file for the sorting the whole data? – Ravi Feb 22 at 12:37
  • I have simplified the command and got the expected result: sort -t '|' -r -k5.8,5.18 -n -k3 completefile.txt 1. Applied length wise sort on 5th column and 2. number sort on 3rd column. (Temporary solution). – Ravi Feb 22 at 13:15
  • @Ravi, why not just sort -t '|' -k5 ? – JJoao Feb 22 at 16:50
  • it didn't sorted properly with the sort -t '|' -k5 command, I thought it is due to the field is looks like a string contains both char and numeric values. – Ravi Feb 25 at 16:05
0

I have simplified the command and got the expected result:

sort -t '|' -r -k5.8,5.18 -n -k3 completefile.txt 
  1. Applied length wise sort on 5th column and
  2. number sort on 3rd column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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