Sammitch
Sammitch

Reputation: 32232

Sort file by one key only

I have large log file comprised of input from many sources, with each line prefixed with the hostname. The log is the output of operations happening in parallel across many hosts, so the logs are somewhat jumbled together.

What I'd like to do is sort the logs by hostname and nothing else so that the events for each server still show up natural order. The sort docs below seem to imply that -k1,1 should accomplish this, but still result in the lines being fully sorted.

   -k, --key=POS1[,POS2]
          start a key at POS1 (origin 1), end it at POS2 (default end of line)

I've made a simple test file:

1 grape
1 banana
2 orange
3 lemon
1 apple

and the expected output would be:

1 grape
1 banana
1 apple
2 orange
3 lemon

But the observed output is:

$ sort -k1,1 sort_test.txt
1 apple
1 banana
1 grape
2 orange
3 lemon

Upvotes: 1

Views: 234

Answers (1)

Hasan Rumman
Hasan Rumman

Reputation: 597

sort -s -k 1,1 sort_test.txt

The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.

Upvotes: 4

Related Questions