Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by Henning Kockerbeck for Prevent sort from sorting filenames instead of greped content

You need to tell sort which part of its input you want to sort after. That's what the switch -k (for "key") is for. Let's say you have a list like

/file3 111/file2 666/file1 333

You want to sort after the second field, so you need -k 2:

sort -r -k 2

By default, sort separates the fields by a "non-blank to blank transition", or between every non-whitespace character which is followed by a whitespace character. In your case, you separate the fields of your input with a :. So have to tell sort that as well, with the switch -t:

sort -r -k 2 -t ':'

As a more general remark, in a pipeline (multiple commands chained together with |), a later command doesn't know how its input came to be. So thinking about it like "sort needs to look at what came from grep and not from find" is probably not that helpful ;) sort only knows what its input looks like, not who played what part in the construction of the input.


Viewing all articles
Browse latest Browse all 2

Trending Articles