Issue
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.
Solution
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 numbertr '\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 Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.