bash - 排序变量

标签 bash sorting unix

我在变量排序时遇到问题。

我使用 Bash 和 NSH 控制台。

我加载到操作系统中最大的可变文件夹:

vdirList=$(nexec -e "find /etc -type d -size +1k -print0 | xargs -0 du -h --max-depth 3 --time --time-style +'%F %T %z' 2>/dev/null| sort -h -k1 2>/dev/null| tail -7 | sed -n '$ ! p' | uniq -c | tac | sed 's/^/BAORES: /'") 
vdirList+="\n"
vdirList+=$(nexec -e "find /usr -type d -size +1k -print0 | xargs -0 du -h --max-depth 3 --time --time-style +'%F %T %z' 2>/dev/null| sort -h -k1 2>/dev/null| tail -7 | sed -n '$ ! p' | uniq -c | tac | sed 's/^/BAORES: /'")
vdirList+="\n"

这是输出:

BAORES: 3 22M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted
BAORES: 2 22M 2017-05-05 14:53:20 +0300 /etc/selinux
BAORES: 1 13M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted/modules
BAORES: 2 1.9G 2018-12-20 05:49:04 +0200 /usr/lib
BAORES: 3 1.3G 2018-12-20 05:48:55 +0200 /usr/lib/x86_64-linux-gnu
BAORES: 1 445M 2018-12-20 05:49:04 +0200 /usr/lib/i386-linux-gnu

我想按 3 列对此文件进行排序,我使用此命令:

dirList=$(nexec -e "echo \"$vdirList\" | sort -hrk3 | head -n 10")

但我的输出是:

BAORES:  3  22M   2017-05-05  14:53:20  +0300  /etc/selinux/targeted
BAORES:  3  1.3G  2018-12-20  05:48:55  +0200  /usr/lib/x86_64-linux-gnu
BAORES:  2  22M   2017-05-05  14:53:20  +0300  /etc/selinux
BAORES:  1  445M  2018-12-20  05:49:04  +0200  /usr/lib/i386-linux-gnu
BAORES:  1  13M   2017-05-05  14:53:20  +0300  /etc/selinux/targeted/modules
BAORES:  2  1.9G  2018-12-20  05:49:04  +0200  /usr/lib

有人可以帮忙解决这个问题吗?谢谢。

最佳答案

我可以使用LC_ALL=en_US.UTF-8确认奇怪的行为。在 C 语言环境下,它似乎可以正常工作:

$ input='BAORES: 3...
...
.../i386-linux-gnu'
$ LC_ALL=C sort -k3,3hr <<< "$input"
BAORES: 2 1.9G 2018-12-20 05:49:04 +0200 /usr/lib
BAORES: 3 1.3G 2018-12-20 05:48:55 +0200 /usr/lib/x86_64-linux-gnu
BAORES: 1 445M 2018-12-20 05:49:04 +0200 /usr/lib/i386-linux-gnu
BAORES: 2 22M 2017-05-05 14:53:20 +0300 /etc/selinux
BAORES: 3 22M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted
BAORES: 1 13M 2017-05-05 14:53:20 +0300 /etc/selinux/targeted/modules

同样有效的方法是显式指定分隔符:

sort -t' ' -k3,3hr

但我发现区域设置更可靠。

关于bash - 排序变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870402/

相关文章:

linux - Cat 数千个文件

c - 如何只打印排序数组的第二个元素?

c - 我在查看 <unistd.h> 中定义的 read() 函数代码时遇到了麻烦

xml - 使用 xmllint 返回多个结果

linux - 使用 sed, tr, ... 修复文件结构

linux - 如何使用 Bash 脚本拆分换行符?

unix - 如何对 AWK 中的列求和?

algorithm - 合并排序比插入排序更快的方式让我困惑

arrays - 对作为哈希值的数组的数组进行排序

linux - ssh 命令输出以保存在 shell 脚本中的文本文件中