为什么这个正则表达式不能在linux上使用find命令

问题描述 投票:1回答:1

我正在尝试执行此命令

find /home/scratch/test_cases/deleted/2nmW1wDfb5/ -iregex '.*/moodys_munis_full_refresh_flat_file_\d+.zip' -type f

它找不到任何文件,但是当我做ls时文件就在那里

ls /home/scratch/test_cases/deleted/2nmW1wDfb5/moodys_munis_full_refresh_flat_file_2.zip

结果是

/home/scratch/test_cases/deleted/2nmW1wDfb5/moodys_munis_full_refresh_flat_file_2.zip
regex linux
1个回答
1
投票

尝试:

find ... -iregex '.*[0-9]+.zip' -type f

代替。或者跑

find -regextype --help
find: Unknown regular expression type '--help'; valid types are 'findutils-default', 'awk', 'egrep', 'ed', 'emacs', 'gnu-awk', 'grep', 'posix-awk', 'posix-basic', 'posix-egrep', 'posix-extended', 'posix-minimal-basic', 'sed'.

获取系统上已知-regextypes的列表,然后尝试

find ... -regextype sed -iregex '.*[0-9]+.zip' -type f

以及其他选项,如果您不知道,哪种风格代表什么。

在我的系统上,\ d不适用于任何选项:

for rt in "findutils-default" "awk" "egrep" "ed" "emacs" "gnu-awk" "grep" "posix-awk" "posix-basic" "posix-egrep" "posix-extended" "posix-minimal-basic" "sed"
do 
   echo -n $rt" "
   find . -type f -regextype $rt -iregex '.*ro.*2\d+.scala'
   echo
done

findutils-default 
awk 
egrep 
ed 
emacs 
gnu-awk 
grep 
posix-awk 
posix-basic 
posix-egrep 
posix-extended 
posix-minimal-basic 
sed 

而大约50%的矩形括号被理解:

for rt in "findutils-default" "awk" "egrep" "ed" "emacs" "gnu-awk" "grep" "posix-awk" "posix-basic" "posix-egrep" "posix-extended" "posix-minimal-basic" "sed"
do 
   echo -n $rt" "
   find . -type f -regextype $rt -iregex '.*ro.*2[0-9]+.scala'
   echo
done

findutils-default ./rot1-25.scala

awk ./rot1-25.scala

egrep ./rot1-25.scala

ed 
emacs ./rot1-25.scala

gnu-awk ./rot1-25.scala

grep 
posix-awk ./rot1-25.scala

posix-basic 
posix-egrep ./rot1-25.scala

posix-extended ./rot1-25.scala

posix-minimal-basic 
sed 
© www.soinside.com 2019 - 2024. All rights reserved.