0

I'm using grep to search over several log files. In the log each line has the time in the format hh:mm:ss as the first part. This time should be the sorting-criteria.

If I'm using grep -r string1 | grep --color string2 | sort the sort will be applied to the filename (which is useful information), adding the -n parameter makes things even worse as the included line number is going to be used.

The logs have a form like that:

354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign 
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl

Edit: The sort I expect to be is:

379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign 
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl

With the suggested change and add of | sort -t'/' -k2V most of the results seem reasonable, yet I got something like this out of the blue while most of the other output is perfectly fine.

353619:d1.com/core-01.log:14:09:45 INFO:          Server: updating
33:c5.com/client-01.log:13:30:02 INFO:       Item: created widget in ACTIVE
  • is the sample shown output of grep or sort? please also add expected output for clarity.. if your sort has -V option, I think sort -t'/' -k2V is what you are looking for – Sundeep Apr 26 '18 at 9:19
  • @Sundeep - the provided output is just regular grep. The sort applies an order to the first part of the line, which is either filename or line number. The change you provided surely did something and the output is mostly sorted (which is helpful). Strangely not everything, which is a little strange. How do I know if my sort has a -V option? – Qohelet Apr 26 '18 at 9:34
  • well, if you didn't get an error.. then -V is supported.. to help you better, please add expected output and/or improve the sample for testing purposes – Sundeep Apr 26 '18 at 9:53
  • @Sundeep - added – Qohelet Apr 26 '18 at 10:01
1

Given the file

354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
353619:d1.com/core-01.log:14:09:45 INFO:          Server: updating
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl

the command

sort -t: -k3 file

would perform a lexicographical sort using the third :-delimited field (and anything after it) as the sorting key. The result would be

379144:c3.com/client-01.log:13:30:02 INFO:       Item: created widget in ARR
354058:c1.com/client-01.log:13:30:04 INFO:       Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO:          change transfer to contact
367422:c5.com/client-01.log:13:51:56 INFO:       searching for Sign
392162:c3.com/client-01.log:13:51:59 INFO:       Item: created widget in ARR
367446:c5.com/client-01.log:13:51:59 INFO:       Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO:          change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO:          change workflow to tgl
353619:d1.com/core-01.log:14:09:45 INFO:          Server: updating

For this to work, it is assumed that the numbers in field 3, 4 and 5 are always zero-filled.

  • How would be the command after the pipe (what to be used instead of file)? Also as seen from the edit it can happen the string appears only in the 33rd line. – Qohelet Apr 26 '18 at 11:14

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.