【问题标题】:Sorting variable排序变量
【发布时间】:2019-05-21 01:50:58
【问题描述】:

我在变量中排序有问题。

我使用 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

有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: bash sorting unix


    【解决方案1】:

    我可以用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
    

    但我发现语言环境设置更可靠。

    【讨论】:

    • 是的,它可以工作,如果它在文件中有这个输出,但我需要在变量中使用它。
    • LC_ALL=C sort -k3,3hr &lt;&lt;&lt; "$variable"
    • 还是一样的输出:-/
    • @Destrem:更新以准确显示适合我的步骤。
    猜你喜欢
    • 2022-12-16
    • 2014-10-08
    • 2014-02-09
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多