Reputation: 13695
I have a file that looks like this
12;6
2;4
9;4
...
In this case the field (column) delimiter is a ";". I want to sort the fields in each line. An acceptable output would be:
6;12
2;4
4;9
An acceptable solution can assume that the field delimiter is a ";" and the values are integers. An ideal solution is more flexible, allowing different delimiters and for alphanumeric sorting.
This all needs to be done on the command line.
Upvotes: 0
Views: 633
Reputation: 212238
perl -wne '$,=";"; chop;
print sort { $a <=> $b } split ";";
print "\n"' input
If your perl isn't ancient:
perl -wnE '$,=";"; chop;
say sort { $a <=> $b } split ";"' input
You can also do:
perl -F\; -wanE 'chop $F[-1]; $,=";"; say sort { $a <=> $b } @F' input
Upvotes: 3
Reputation: 195039
awk 'BEGIN{FS=OFS=";"}{if($1>$2)print $2,$1;else print $1,$2;}' file
test
kent$ cat t.txt
12;6
2;4
9;4
ccc;aaa
bab;baa
kent$ awk 'BEGIN{FS=OFS=";"}{if($1>$2)print $2,$1;else print $1,$2;}' t.txt
6;12
2;4
4;9
aaa;ccc
baa;bab
Upvotes: 2