tags:

views:

239

answers:

7

Is there a Linux utility of a bash command I can use to sort a space delimited string of numbers?

+5  A: 

Here's a simple example to get you going:

echo "81 4 6 12 3 0" | tr " " "\n" | sort -g

tr translates the spaces delimiting the numbers, into carriage returns, because sort uses carriage returns as delimiters (ie it is for sorting lines of text). The -g option tells sort to sort by "general numerical value".

man sort for further details about sort.

James Morris
A: 
$ s=(one two three four)
$ sorted=$(printf "%s\n" ${s[@]}|sort)
$ echo $sorted
four one three two
ghostdog74
A: 

This is a variation on ghostdog74's answer that's too big to fit in a comment. It shows digits instead of names of numbers and both the original string and the result are in space-delimited strings (instead of an array which becomes a newline-delimited string).

$ s="3 2 11 15 8"
$ sorted=$(echo $(printf "%s\n" $s | sort -n))
$ echo $sorted
2 3 8 11 15
$ echo "$sorted"
2 3 8 11 15

If you didn't use the echo when setting the value of sorted, then the string has newlines in it. In that case echoing it without quotes puts it all on one line, but, as echoing it with quotes would show, each number would appear on its own line. This is the case whether the original is an array or a string.

# demo
$ s="3 2 11 15 8"
$ sorted=$(printf "%s\n" $s | sort -n)
$ echo $sorted
2 3 8 11 15
$ echo "$sorted"
2
3
8
11
15
Dennis Williamson
A: 

Using Bash parameter expansion (to replace spaces with newlines) we can do:

str="3 2 11 15 8" 
sort -n <<< "${str// /$'\n'}"

# alternative
NL=$'\n'
str="3 2 11 15 8"
sort -n <<< "${str// /${NL}}"
trevvor
A: 

If you actually have a space-delimited string of numbers, then one of the other answers provided would work fine. If your list is a bash array, then:

oldIFS="$IFS"
IFS=$'\n'
array=($(sort -g <<< "${array[*]}"))
IFS="$oldIFS"

might be a better solution. The newline delimiter would help if you want to generalize to sorting an array of strings instead of numbers.

Evan Krall
A: 

awk 'BEGIN{split(ARGV[1], numbers);for(i in numbers) {print numbers[i]} }' "6 7 4 1 2 3" | sort -n

Fakrudeen
A: 

Improving on Evan Krall's nice Bash "array sort" by limiting the scope of IFS to a single command:

printf "%q\n" "${IFS}"
array=(3 2 11 15 8) 
array=($(IFS=$'\n' sort -n <<< "${array[*]}")) 
echo "${array[@]}" 
printf "%q\n" "${IFS}"
bashfu