With gawk
(GNU awk
) for the asort()
function:
gawk -v SEP='*' '{ i=0; split($0, arr, SEP); len=asort(arr);
while ( ++i<=len ){ printf("%s%s", i>1?SEP:"", arr[i]) };
print ""
}' infile
replace *
as the field separator in SEP='*'
with your delimiter.
You can also do with the following command in case of a single line (because it's better leave it alone of using shell-loops for text-processing purposes)
tr '.' '\n' <<<"$aline" | sort -n | paste -sd'.' -
replace dots .
with your delimiter.
add -u
to the sort
command above to remove the duplicates.
Notes:
You may need to use -g, --general-numeric-sort
option of sort
instead of -n, --numeric-sort
to handle any class of numbers (integer, float, scientific, Hexadecimal, etc).
$ aline='2e-18,6.01e-17,1.4,-4,0xB000,0xB001,23,-3.e+11'
$ tr ',' '\n' <<<"$aline" |sort -g | paste -sd',' -
-3.e+11,-4,2e-18,6.01e-17,1.4,23,0xB000,0xB001
In awk
no need change, it still will handling those.
Best Answer
Use e.g.
sed
to replace the string with a one-character delimiter, sort by the column, and then replace the delimiter back again:This assumes that there is a character that you know doesn't appear in the input. A control character would be a common candidate, but you need to make a choice based on your knowledge of the input format.