While -k option in sort command helps to sort the data by given column but below example which is not perfectly tabular is still sorted by the column. How does it do it ? Which character it assumed in place of empty cells ?

test@ubuntu:~/Documents$ cat file1
a b c

test@ubuntu:~/Documents$ sort -k 2 file1
a b c
  • The "empty cells" are empty strings, and the empty string ("") sorts before any non-empty string in a lexicographical sort. There's no need to assume any character. This feels like a leading "trick" question -- sorry if you're genuinely confused and I'm just too suspicious ;-) – Uncle Billy Jan 28 at 21:29
  • @UncleBilly I disagree. If you perform cat -A file, you will see that the entire line of d and e are d$, e$. – schrodigerscatcuriosity Jan 28 at 21:39
  • @shr what are you disagreeing with? – Uncle Billy Jan 28 at 21:40
  • @UncleBilly If I understand correctly, you say "The "empty cells" are empty strings", I interpret that they are no empty strings, the line has ended. – schrodigerscatcuriosity Jan 28 at 21:42
  • @shr the OP is already (correctly) assuming that "missing cells" are treated as "empty cells". The question doesn't seem to be about that. – Uncle Billy Jan 28 at 21:54

Sort command sorts file assuming the contents are ASCII. So null must have been used there.

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.