简体   繁体   English

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

[英]Sorting space delimited numbers with Linux/Bash

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

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). tr将分隔数字的空格转换为回车符,因为sort使用回车符作为分隔符(即用于排序文本 )。 The -g option tells sort to sort by "general numerical value". -g选项告诉sort按“一般数值”排序。

man sort for further details about sort . man sort有关sort更多细节。

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 -n1 to convert to new lines. 而不是tr ,我使用xargs -n1转换为新行。 The final xargs is to convert back, to a space separated sequence of numbers. 最终的xargs是转换回空格分隔的数字序列。

This is a variation on ghostdog74's answer that's too big to fit in a comment. 这是ghostdog74的答案的一个变种,它太大而不适合评论。 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. 如果在设置sorted的值时没有使用echo ,则该字符串中包含换行符。 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
$ s=(one two three four)
$ sorted=$(printf "%s\n" ${s[@]}|sort)
$ echo $sorted
four one three two

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}}"

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. 如果要概括排序字符串数组而不是数字,换行符分隔符将有所帮助。

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

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}"

I added this to my .zshrc (or .bashrc ) file:我将此添加到我的.zshrc (或.bashrc )文件中:

#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.感谢@FranMowinckel 和其他人的启发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM