gpt4 book ai didi

linux - 根据文件名中的数字索引对目录中的文件进行排序

转载 作者:太空狗 更新时间:2023-10-29 11:05:44 26 4
gpt4 key购买 nike

我在我的脚本中使用了一个 for 循环,就像这样......

 for var in ipsec_packet*.txt; do
echo $var
done

输出

ipsec_packet10.txt
ipsec_packet11.txt
ipsec_packet12.txt
ipsec_packet13.txt
ipsec_packet14.txt
ipsec_packet15.txt
ipsec_packet16.txt
ipsec_packet17.txt
ipsec_packet18.txt
ipsec_packet1.txt
ipsec_packet2.txt
ipsec_packet3.txt
ipsec_packet4.txt
ipsec_packet5.txt
ipsec_packet6.txt
ipsec_packet7.txt
ipsec_packet8.txt
ipsec_packet9.txt

但我希望它们按照这样的排序顺序从 1 开始到最大的可用值(这里是 18)..

ipsec_packet1.txt
ipsec_packet2.txt
...
...
ipsec_packet18.txt

我尝试了 sort -n k14 但它没有帮助。请建议我一些可以帮助我的排序变体或任何其他 bash/awk 功能。

最佳答案

您可以尝试将 sort-V 选项一起使用,用于文本中数字的自然排序:

for var in `ls ipsec_packet*.txt | sort -V`; do  echo $var; done

关于linux - 根据文件名中的数字索引对目录中的文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8210733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com