簡體   English   中英

為什么這個正則表達式不能在 linux 上使用 find 命令

[英]why this regex is not working with find command on linux

我正在嘗試執行此命令

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

嘗試:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM