4

I have a log file and I'm looking to sort according to response time, 4th field:

GET /api/user/john 200 0.194 ms - 7307

But it contains color tags, here's the output from vi:

^[[0mGET /api/user/john ^[[32m200 ^[[0m0.194 ms - 7307^[[0m

Is there a simple way to do this?

2
  • 3
    Why does your log file contain these? What tool is generating it? Can't you change that instead?
    – terdon
    May 26 '20 at 12:54
  • This is generated by PM2, and it's inserting the colors so sysadmin can see what's happening more easily. Probably.
    – d9ngle
    May 26 '20 at 13:01
3
  1. Extract the field you want to sort on (typically with cut, sed or awk), and strip off its formatting escape sequences. You can find scripts for the second part in Removing control chars (including console codes / colours) from script output. I'll use uncolor below to stand for one of these scripts.
  2. Collate the result with the original (paste). Use a separator character that doesn't appear in the data to sort.
  3. Sort.
  4. Remove the sort key.

For example, if your fields are tab-separated:

<input-file.txt cut -f 4 | uncolor |
paste - input-file.txt |
sort |
cut -f 2-
2
  • uncolor was all I needed, I can now simply do tail server.log | uncolor | sort -g -k 4,4
    – d9ngle
    May 26 '20 at 13:10
  • sort needs to be sort -g
    – d9ngle
    May 26 '20 at 13:19

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.