Linux Bash中根据字段的数值排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4856030/
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
Sorting according to field's numerical value in Bash
提问by lukmac
Example:content of File.txt:
示例:File.txt 的内容:
100 foo
2 bar
300 tuu
When using 'sort -k 1,1 File.txt', the order of lines will not changed, though we are expecting :
当使用 'sort -k 1,1 File.txt' 时,行的顺序不会改变,但我们期望:
2 bar
100 foo
300 tuu
How can we sort a field consisting numbers based on the absolute numericalvalue?
我们如何根据绝对数值对由数字组成的字段进行排序?
采纳答案by Andrew White
Take a peek at the man page for sort...
看一看手册页进行排序...
-n, --numeric-sort compare according to string numerical value
-n, --numeric-sort compare according to string numerical value
So here is an example...
所以这是一个例子......
sort -n filename
回答by Roman Cheplyaka
Use sort -n
or sort --numeric-sort
.
使用sort -n
或sort --numeric-sort
。
回答by Kai Sternad
You have to use the numeric sort option:
您必须使用数字排序选项:
sort -n -k 1,1 File.txt
回答by pgilmon
Well, most other answers here refer to
嗯,这里的大多数其他答案都是指
sort -n
However, I'm not sure this works for negative numbers. Here are the results I get with sort version 6.10 on Fedora 9.
但是,我不确定这是否适用于负数。以下是我在 Fedora 9 上使用排序版本 6.10 得到的结果。
Input file:
输入文件:
-0.907928466796875
-0.61614990234375
1.135406494140625
0.48614501953125
-0.4140167236328125
Output:
输出:
-0.4140167236328125
0.48614501953125
-0.61614990234375
-0.907928466796875
1.135406494140625
Which is obviously not ordered by numeric value.
这显然不是按数值排序的。
Then, I guess that a more precise answer would be to use sort -n
but only if all the values are positive.
然后,我想更精确的答案是使用,sort -n
但前提是所有值都是正数。
P.S.: Using sort -g
returns just the same results for this example
PS:使用sort -g
本示例返回相同的结果
Edit:
编辑:
Looks like the locale settings affect how the minus sign affects the order (see here). In order to get proper results I just did:
看起来语言环境设置会影响减号如何影响顺序(请参阅此处)。为了获得正确的结果,我只是做了:
LC_ALL=C sort -n filename.txt
回答by karthik
echo " Enter any values to sorting: "
read n
i=0;
t=0;
echo " Enter the n value: "
for(( i=0;i<n;i++ ))
do
read s[$i]
done
for(( i=0;i<n;i++ ))
do
for(( j=i+1;j<n;j++ ))
do
if [ ${s[$i]} -gt ${s[$j]} ]
then
t=${s[$i]}
s[$i]=${s[$j]}
s[$j]=$t
fi
done
done
for(( i=0;i<n;i++ ))
do
echo " ${s[$i]} "
done
回答by Aarish Ramesh
回答by TMG
If you are sorting strings that are mixed text & numbers, for example filenames of rolling logs then sorting with sort -n
doesn't work as expected:
如果您正在对混合文本和数字的字符串进行排序,例如滚动日志的文件名,则排序sort -n
方式无法按预期工作:
$ ls |sort -n
output.log.1
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.2
output.log.20
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
In that case option -V
does the trick:
在这种情况下,选项-V
可以解决问题:
$ ls |sort -V
output.log.1
output.log.2
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.20
from man page:
从手册页:
-V, --version-sort natural sort of (version) numbers within text
-V, --version-sort natural sort of (version) numbers within text
回答by amc
You must do the following command:
您必须执行以下命令:
sort -n -k1 filename
sort -n -k1 filename
That should do it :)
那应该这样做:)