0

Need help with the UNIX command below to sort the file by the following words, from major sort key to minor sort key: word 3, word 1, word 4. (words are separated by white spaces)

sort -t -k3 -k1 -k4 > newfile.txt doesnt work

migrated from stackoverflow.com Mar 21 '13 at 10:56

This question came from our site for professional and enthusiast programmers.

3

Short answer is, get rid of the -t or specify -t" " or similar to specify a single character as your delimiter.

So: sort -k3 -k1 -k4 in.txt > newfile.txt

In detail...

in.txt

one two three four
the quick brown fox
jumps over the lazy dog
pun poo three poor
one two three four five
the elegant brown zebra

bash cmd

  • You are sorting words, so just I'm showing how to specify the -d option for dictionary sort. You could use numeric sort with the -g option for other situations.
  • You want whitespace - this is what sort uses by default. Otherwise you'd need to use -t"," or similar.
  • So the cmd is:

    sort -k3,3d -k1,1d -k4,4d in.txt > newfile.txt

newfile.txt

the quick brown fox
the elegant brown zebra
jumps over the lazy dog
one two three four
one two three four five
pun poo three poor
  • Thanx azhrei. That was fast. Btw what does the d argument stands for ? – user2180912 Mar 20 '13 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy