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

我在我的脚本中使用了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功能.

解决方法:

您可以尝试对-V选项使用排序,该选项用于对文本中的数字进行自然排序:

for var in `ls ipsec_packet*.txt | sort -V`; do  echo $var; done
上一篇:mysql-复杂的sql分组和排序


下一篇:PHP usort的有害行为