I have some numbers like
7, 15, 6, 2, -9
I want to sort it like this in bash(from command line or either a script file)
-9, 2, 6, 7, 15
How can I do this? I am unable to get this using sort command.
echo "7, 15, 6, 2, -9" | sed -e $'s/,/\\\n/g' | sort -n | tr '\n' ',' | sed 's/.$//'
sed -e $'s/,/\\\n/g': For splitting string into lines by comma.
sort -n: Then you can use sort by number
tr '\n' ',': Covert newline separator back to comma.
sed 's/.$//': Removing tailing comma.
Not elegant enough, but it should work :p
Answered By – Yu-Lin Chen