I have a fixed-width-field file which I'm trying to sort using the UNIX (Cygwin, in my case) sort utility.
The problem is there is a two-line header at the top of the file which is being sorted to the bottom of the file (as each header line begins with a colon).
Is there a way to tell sort either "pass the first two lines across unsorted" or to specify an ordering which sorts the colon lines to the top - the remaining lines are always start with a 6-digit numeric (which is actually the key I'm sorting on) if that helps.
Example:
:0:12345
:1:6:2:3:8:4:2
010005TSTDOG_FOOD01
500123TSTMY_RADAR00
222334NOTALINEOUT01
477821USASHUTTLES21
325611LVEANOTHERS00
should sort to:
:0:12345
:1:6:2:3:8:4:2
010005TSTDOG_FOOD01
222334NOTALINEOUT01
325611LVEANOTHERS00
477821USASHUTTLES21
500123TSTMY_RADAR00
This is the same as Ian Sherbin answer but my implementation is :-
example:
Here is a version that works on piped data:
If your header has multiple lines:
This solution is from here
The parentheses create a subshell, wrapping up the stdout so you can pipe it or redirect it as if it had come from a single command.
It only takes 2 lines of code...
For a numeric data, -n is required. For alpha sort, the -n is not required.
Example file:
$ cat test.txt
Result:
$ cat a.tmp
With Python: