Linux file sort with incomplete columns

By : frixhax
Source: Stackoverflow.com
Question!

I need to sort a file by the first column, if there is a numerical entry, and by the second column when there is none in the first. It looks basically like this:

3 - foo bar
1 1 foo bar
- 5 foo bar
2 2 foo bar
- 4 foo bar

and I need the output then to be

1 1 foo bar
2 2 foo bar
3 - foo bar
- 4 foo bar
- 5 foo bar

Is there any nice way to do that in linux with a single command? Thanks!

By : frixhax


Answers

On output that follows that format you can just use the basic sort command:

sort -k 1,2 foo.txt
By : teppic


This video can help you solving your question :)
By: admin