$ cat file.txt chrom:index:forward:reverse chr01:13:1:2 chr03:12:1:4 chr01:3445:1:6 chr02:2311:3:1 chr13:23432:4:7 chr01:212:5:2 chr02:345:12:6 chr01:45:45:0Output required:
chrom:index:forward:reverse chr01:13:1:2 chr01:45:45:0 chr01:212:5:2 chr01:3445:1:6 chr02:345:12:6 chr02:2311:3:1 chr03:12:1:4 chr13:23432:4:7i.e. Sort file.txt ignoring the header line (1st line).
Sort first field in ascending order (string)
Sort second field in ascending order (numeric)
One of the solution would be:
$ head -1 file.txt ; tail -n +2 file.txt | sort -t : -k1,1 -k2,2nAs you can see two commands are combined together to satisfy the output.
i) First part is printing the first line of file.txt
ii) Second part is printing the lines except first line and then sorting the lines based on first field (string) and second field (numeric sort).
- Unix sort file based on last field
- Sort strings by length using Awk
- Unix sort date in ddmmyyyy format using Awk