使用 Linux/Bash 对空格分隔的数字进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2625976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:57:13  来源:igfitidea点击:

Sorting space delimited numbers with Linux/Bash

linuxbash

提问by syker

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

是否有 Linux 实用程序或 Bash 命令可用于对以空格分隔的数字字符串进行排序?

回答by James Morris

Here's a simple example to get you going:

这是一个让你开始的简单例子:

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

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

trtranslates the spaces delimiting the numbers, into carriage returns, because sort uses carriage returns as delimiters (ie it is for sorting linesof text). The -goption tells sort to sort by "general numerical value".

tr将分隔数字的空格转换为回车,因为 sort 使用回车作为分隔符(即它用于对文本进行排序)。该-g选项告诉 sort 按“一般数值”排序。

man sortfor further details about sort.

man sort有关 的更多详细信息sort

回答by FranMowinckel

This is a variation from @JamesMorris answer:

这是@JamesMorris 答案的变体:

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

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

而不是tr,我xargs -n1用来转换为新行。最后xargs是转换回来,以空格分隔的数字序列。

回答by Paused until further notice.

This is a variation on ghostdog74'sanswer 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).

这是ghostdog74答案的一个变体,它太大而无法放入评论中。它显示数字而不是数字名称,并且原始字符串和结果都在以空格分隔的字符串中(而不是成为换行符分隔的字符串的数组)。

$ 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 echowhen 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.

如果您echo在设置 的值时未使用sorted,则字符串中包含换行符。在这种情况下,不带引号的回显会将其全部放在一行上,但是,正如用引号回显所显示的那样,每个数字都将出现在自己的行上。无论原始是数组还是字符串,都是这种情况。

# 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

回答by ghostdog74

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

回答by trevvor

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

使用 Bash 参数扩展(用换行符替换空格)我们可以:

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

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

回答by Evan Krall

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:

如果您确实有一个以空格分隔的数字字符串,那么提供的其他答案之一就可以正常工作。如果您的列表是 bash 数组,则:

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.

可能是更好的解决方案。如果您想对字符串数组而不是数字进行排序,换行符将有所帮助。

回答by Fakrudeen

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

回答by bashfu

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

通过将 IFS 的范围限制为单个命令来改进 Evan Krall 漂亮的 Bash“数组排序”:

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