如何根据字段的数值对文件进行排序?

发布于 2024-10-15 04:34:02 字数 233 浏览 4 评论 0原文

示例file.txt

  100 foo
  2 bar
  300 tuu

当使用sort -k 1,1 file.txt时,行的顺序不会改变,尽管我们期望:

  2 bar
  100 foo
  300 tuu

如何对包含的字段进行排序基于绝对数值的数字?

Example file.txt:

  100 foo
  2 bar
  300 tuu

When using sort -k 1,1 file.txt, the order of lines will not change, though we are expecting :

  2 bar
  100 foo
  300 tuu

How to sort a field consisting of numbers based on the absolute numerical value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

耳钉梦 2024-10-22 04:34:02

查看排序手册页...

<前><代码> -n, --数字排序
根据字符串数值进行比较

所以这是一个例子......

sort -n filename

Take a peek at the man page for sort...

   -n, --numeric-sort
          compare according to string numerical value

So here is an example...

sort -n filename
流年已逝 2024-10-22 04:34:02

如果您要对混合文本和文本的字符串进行排序,数字,例如滚动日志的文件名,然后使用 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

在这种情况下,选项 -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

来自手册页:

<前><代码> -V, --版本排序
文本中(版本)数字的自然排序

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:

$ 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:

$ 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
冷默言语 2024-10-22 04:34:02

好吧,这里的大多数其他答案都指的是

sort -n

但是,我不确定这是否适用于负数。以下是我在 Fedora 9 上使用排序版本 6.10 得到的结果。

输入文件:

-0.907928466796875
-0.61614990234375
1.135406494140625
0.48614501953125
-0.4140167236328125

输出:

-0.4140167236328125
0.48614501953125
-0.61614990234375
-0.907928466796875
1.135406494140625

显然不是按数值排序的。

然后,我想更精确的答案是使用 sort -n 但前提是所有值都是正数。

PS:使用 sort -g 返回与此示例相同的结果

编辑:

看起来区域设置会影响减号如何影响顺序(参见此处)。为了获得正确的结果,我刚刚做了:

LC_ALL=C sort -n filename.txt

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.

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.

P.S.: Using sort -g returns just the same results for this example

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
不打扰别人 2024-10-22 04:34:02

您必须使用数字排序选项:

sort -n -k 1,1 File.txt

You have to use the numeric sort option:

sort -n -k 1,1 File.txt
骄傲 2024-10-22 04:34:02

使用sort -nsort --numeric-sort

Use sort -n or sort --numeric-sort.

眸中客 2024-10-22 04:34:02

您必须执行以下命令:

sort -n -k1 filename

这样就可以了:)

You must do the following command:

sort -n -k1 filename

That should do it :)

遗弃M 2024-10-22 04:34:02

谢谢你,皮吉尔蒙。

我试图制作一个 m3u 播放列表,对所有目录和子目录进行精确的字母排序,其中一些具有相同的名称和编号。
字母不是问题,数字才是问题。

之前:

album
album2

成为

album2
album

您的LC_ALL=C sort -n以更简单且正确的方式实现了这一点。我读了“排序”帮助/手册中的 LC_ALL=C 但没有太注意它(或者更确切地说不明白如何准确使用它)。

结束代码:

find . -depth -type f -iregex "\(.*mp3\|.*m4a\)" | LC_ALL=C sort -n > myplaylist_ordered.m3u

将(给定的)音乐文件路径按树形顺序写入 m3u。

另一种选择是完全重新排列列表:

find . -depth -type f -iregex "\(.*mp3\|.*m4a\)" | shuf > myplaylist_shuffled.m3u

至少在这种情况下,这些东西目前对我来说似乎很有效。

Thank you pgilmon.

I was trying to make a m3u-playlist with precise alphabetical sorting allover dirs and subdirs of which some with the same name and numbered.
The alphabet wasn't the problem, the numbering was.

Before:

album
album2

became

album2
album

Your LC_ALL=C sort -n did the trick the easier and correct way. I read the LC_ALL=C in the 'sort' help/man but hadn't payed too much attention to it (or rather didn't understand how to exactly use it).

End code:

find . -depth -type f -iregex "\(.*mp3\|.*m4a\)" | LC_ALL=C sort -n > myplaylist_ordered.m3u

writes (the given) music file paths in their tree order to the m3u.

The alternative is a completely shuffled list:

find . -depth -type f -iregex "\(.*mp3\|.*m4a\)" | shuf > myplaylist_shuffled.m3u

At least these things seem to work well for me for the moment in this context.

盗梦空间 2024-10-22 04:34:02

使用 sort -nr 按降序排序。请参阅

排序

请参阅上面的手册页以获取更多参考

Use sort -nr for sorting in descending order. Refer

Sort

Refer the above Man page for further reference

为你拒绝所有暧昧 2024-10-22 04:34:02
    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
    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文