Consider using perl
for this:
perl -ape '@F=sort @F;$_="@F\n"' input.txt
Here -a
turns on automatic field splitting (like awk
does) into the array @F, -p
makes it execute the script for each line and print $_
each time, and -e
specifies the script directly on the command line.
Not quite 6 characters, I'm afraid, Sean.
This should have been simple in awk, but it doen't quite have the features needed. If there had been an array $@
corresponding to the fields $1
, $2
, etc., then the solution would have been awk '{asort $@}' input.txt
, but sadly no such array exits. The loops required to move the fields into an array and out of it again make it longer than the bash version:
awk '{for(i=1;i<=NF;i++)a[i]=$i;asort(a);for(i=1;i<=NF;i++)printf("%s ",a[i]);printf("\n")}' input.txt
So awk
isn't the right tool for the job here. It's also a bit odd that sort
itself doesn't have a switch to control its sorting direction.