根据文件名中的数字索引对目录中的文件进行排序
我在我的脚本中使用了这样的 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 功能。
I have used a for loop in my script like this ...
for var in ipsec_packet*.txt; do
echo $var
done
Output
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
but I want them starting from 1 to the largest avaliable (here 18) in sorted order like this ..
ipsec_packet1.txt
ipsec_packet2.txt
...
...
ipsec_packet18.txt
I tried sort -n k14
but it did not help. Please suggest me some variation of sort or any other bash/awk feature which could help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试将
sort
与-V
选项一起使用,用于对文本中的数字进行自然排序:You can try using
sort
with the-V
option used for natural sorting of numbers within text:如果GNU排序不可用(即-V不受支持)并且如果Perl可接受:
或者:
If GNU sort is not available (i.e. -V isn't supported) and if Perl is acceptable:
Alternatively:
我更喜欢codaddict的解决方案,但这里有一个
sed
+sort
技巧,用于当您的sort
没有-V
:您可以将
\t
替换为输入数据中未出现的任何单个字符,并将该字符指定为sort
的字段分隔符为-t
。I would prefer codaddict's solution, but here is a
sed
+sort
trick for when yoursort
doesn't have-V
:You can replace
\t
with any single character that does not appear in your input data and specify that character assort
's field separator with-t
.这可能对你有用:
This might work for you: