Bash 列出高于特定数字的数字文件

Bash 列出高于特定数字的数字文件,bash,shell,awk,grep,Bash,Shell,Awk,Grep,我有目录中的文件列表,以数字结尾。详情如下: play_football_3 play_football_4 play_football_5 play_football_15 play_football_59 我能够提取上述文件的最后一个数字。作为 echo "play_football_5" | cut -f3 -d"_" 现在我试着列出所有版本比play_football_5更高的文件 预期产出: play_football_15 play_football_59 你可以用 awk -

我有目录中的文件列表,以数字结尾。详情如下:

play_football_3
play_football_4
play_football_5
play_football_15
play_football_59
我能够提取上述文件的最后一个数字。作为

echo "play_football_5" | cut -f3 -d"_"
现在我试着列出所有版本比play_football_5更高的文件

预期产出:

play_football_15
play_football_59
你可以用

awk -F'_' '$3 > 5'


使用
-F''
,您可以将分隔符设置为下划线,使用
'$3>5'
,仅打印第三个字段大于
5

的行(记录),您可以使用
awk

printf "%s\n" play_football_* | awk -F_ '$3>5'

printf
将列出当前目录中以
play\u football\u
开头的所有文件,
awk
过滤数字大于5的文件如果您有
bash
2.02-alpha1或更高版本,您可以打开“扩展全局绑定”并查找以
play_football\u
开头而不是以数字0-5结尾的文件,如下所示:

shopt -s extglob

ls play_football_!([0-5])
for file in $(ls | cut -d_ -f3)
do
  if [[ "$file" -gt 5 ]]
  then
    echo "play_football_$file"
  fi
done

是开始了解更多信息的参考。

您也可以通过以下方式保留您的原始想法:

shopt -s extglob

ls play_football_!([0-5])
for file in $(ls | cut -d_ -f3)
do
  if [[ "$file" -gt 5 ]]
  then
    echo "play_football_$file"
  fi
done
如果在包含文件的目录中执行此操作,您将获得:

play_football_15
play_football_59
使用Perl一行程序

> ll
total 0
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_5
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_4
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_3
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_15
-rw-r--r-- 1 xxxxxx devlgrp 0 Oct 30 14:41 play_football_59
> perl -ne ' BEGIN { @files=glob("play*");foreach(@files){ ($file=$_)=~s/.*_(\d+)/\1/g; print "$_\n" if $file > 5 } exit }'
play_football_15
play_football_59
>

$NF
不是更好一点吗?:)@mickp我认为是数字,所以
$3
$NF
:)更吸引我。嗯,我认为
$NF
对于文件名来说会更通用,比如
play\u foo\u 15
:)@mickp示例数据中没有
play\u foo\u 15
。在OP中使用
cut-f3-d”
意味着
play\u football\u foo\u 15
类似的值是不应该出现的。
extglob
是在Bash 2.02-alpha1中添加的。看见