24

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

codeforester
  • 39,467
  • 16
  • 112
  • 140
syker
  • 10,912
  • 16
  • 56
  • 68
  • [Very related](https://stackoverflow.com/questions/13648410/how-can-i-get-unique-values-from-an-array-in-bash). – user202729 Jul 18 '18 at 10:04

9 Answers9

41

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
  • 4,867
  • 3
  • 32
  • 51
14

This is a variation from @JamesMorris answer:

echo "81 4 6 12 3 0" | xargs -n1 | sort -g | xargs

Instead of tr, I use xargs -n1 to convert to new lines. The final xargs is to convert back, to a space separated sequence of numbers.

FranMowinckel
  • 4,233
  • 1
  • 30
  • 26
  • This is super handy, but why oh why do `xargs -n1` and `xargs` do that? – Jacktose Apr 19 '21 at 23:10
  • `xargs` by default (if no utility specified) uses `echo` on the input and the rest it's just about how it handles arguments: `-n1` it's max one per invocation. – FranMowinckel Apr 22 '21 at 13:05
4

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
  • 346,391
  • 90
  • 374
  • 439
3
$ s=(one two three four)
$ sorted=$(printf "%s\n" ${s[@]}|sort)
$ echo $sorted
four one three two
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

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
  • 11
  • 1
1

I added this to my .zshrc (or .bashrc) file:

#sort a space-separated list of words (e.g. a list of HTML classes)
sortwords() {
  echo $1 | xargs -n1 | sort -g | xargs
}

Call it from the terminal like this:

sortwords "banana date apple cherry"

# apple banana cherry date

Thanks to @FranMowinckel and others for inspiration.

Jeremy Moritz
  • 13,864
  • 7
  • 39
  • 43
0

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
  • 2,915
  • 3
  • 23
  • 20
  • my list is a bash array, but this isn't working for me: `packages="$(sort <<< ${!package2apk[@]})"` – Jayen Jun 16 '14 at 11:00
0
$ awk 'BEGIN{split(ARGV[1], numbers);for(i in numbers) {print numbers[i]} }' \
     "6 7 4 1 2 3" | sort -n
slm
  • 15,396
  • 12
  • 109
  • 124
Fakrudeen
  • 5,778
  • 7
  • 44
  • 70
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Nov 07 '17 at 20:09
0

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
  • 1