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

发布于 2024-12-17 05:47:48 字数 710 浏览 0 评论 0原文

我在我的脚本中使用了这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好菇凉咱不稀罕他 2024-12-24 05:47:48

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

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

You can try using sort with the -V option used for natural sorting of numbers within text:

for var in `ls ipsec_packet*.txt | sort -V`; do  echo $var; done
苄①跕圉湢 2024-12-24 05:47:48

如果GNU排序不可用(即-V不受支持)并且如果Perl可接受:

perl -le'
  print join $/, 
    map $_->[1], 
      sort { 
        $a->[0] <=> $b->[0] 
        } map [/(\d+)\./, $_], 
          glob shift
  ' '*.txt'

或者:

printf '%s\n' *.txt |
  sed 's/.*[^0-9]\([0-9]*\)\./\1,&/' |
    sort -n |
      cut -d, -f2-

If GNU sort is not available (i.e. -V isn't supported) and if Perl is acceptable:

perl -le'
  print join $/, 
    map $_->[1], 
      sort { 
        $a->[0] <=> $b->[0] 
        } map [/(\d+)\./, $_], 
          glob shift
  ' '*.txt'

Alternatively:

printf '%s\n' *.txt |
  sed 's/.*[^0-9]\([0-9]*\)\./\1,&/' |
    sort -n |
      cut -d, -f2-
方圜几里 2024-12-24 05:47:48

我更喜欢codaddict的解决方案,但这里有一个 sed+sort 技巧,用于当您的 sort没有 -V

for var in $(ls ipsec_packet*.txt | sed 's/[0-9]/\t&/' | sort -n -k2 | sed 's/\t//')
do
    echo $var
done

您可以将 \t 替换为输入数据中未出现的任何单个字符,并将该字符指定为 sort 的字段分隔符为 -t

I would prefer codaddict's solution, but here is a sed+sort trick for when your sort doesn't have -V:

for var in $(ls ipsec_packet*.txt | sed 's/[0-9]/\t&/' | sort -n -k2 | sed 's/\t//')
do
    echo $var
done

You can replace \t with any single character that does not appear in your input data and specify that character as sort's field separator with -t.

风向决定发型 2024-12-24 05:47:48

这可能对你有用:

ls -v ipsec_packet*.txt

This might work for you:

ls -v ipsec_packet*.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文