0

I have a text file that I am trying to parse through:

176.9.x.xx - MISS [07/Aug/2017:13:03:59 -0400]  foo.org "GET /foo/bar?page=7 HTTP/1.1" 200 17441 "-" "Mozilla/5.0 (compatible; MegaIndex.ru/2.0; +http://megaindex.com/crawler)" u:192.168.x.x:80 t:0.002

I am really trying to sort by the value t:0.002. ( Not all the values are 0.002, but I am trying to sort this field numerically). Now in a bunch of these entries, I get "t:-" (which I am using the grep -v to ignore).

I know i can use sort but this is where I get stuck:

cat test.txt | grep -v "t:-" | sort -n 

I can't seem to get it to sort by the value "t:". I'm sure I need to add a few more things to the sort command but I'm not sure what. Any suggestions?

3
  • What seems to be the issue? Your command is working for me as posted. Perhaps your other input lines don't match the one you've posted here?
    – jesse_b
    Aug 7, 2017 at 20:18
  • Your edit didn't clarify anything. I have created several lines all with different values for t: and then executed your sort command and it sorts them properly in numeric order based on the value of t: . This is because everything else on the lines are identical therefore the only thing to sort on is the value of t: . So without knowing if all the fields in your input file are identical there isn't much more I can do without making assumptions.
    – jesse_b
    Aug 7, 2017 at 20:20
  • The command is incorrect as it is not sorting by t:<whatevernumbergoesherethatneedstobesorted>
    – ryekayo
    Aug 7, 2017 at 20:22

1 Answer 1

1

If it's more reliable that your "t" field is always the last one, you could use a Schwartzian transform of: decorate, sort, un-decorate:

awk '{print substr($NF, 3), $0}' input | sort -n | cut -d ' ' -f2-

Here, awk is printing the last field, stripped of its leading "t:" text; sort then has a nice numeric value to sort by, then we strip the first field back off.

0

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.