使用find命令的正则表达式

Alp*_*tur 5 regex linux find

我正在尝试使用find命令,-regextype但是它无法正常工作。

我试图找到所有ch文件,并将它们发送到管和grep的名称,func_foo这些文件里面。我想念什么?

$ find ./ -regextype sed -regex ".*\[c|h]" | xargs grep -n --color func_foo
Run Code Online (Sandbox Code Playgroud)

同样在类似的方面,我尝试了以下命令,但它给了我一个错误,例如路径必须在表达式之前:

$ find  ./  -type  f  "*.c"  |  xargs  grep  -n  --color  func_foo
Run Code Online (Sandbox Code Playgroud)

Gen*_*sky 7

接受的答案包含一些错误。在我的系统上,GNU find的联机帮助页指示运行find -regextype help以查看受支持的正则表达式类型的列表。

# 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'.
Run Code Online (Sandbox Code Playgroud)

例如find . -regextype egrep -regex '.*\.(c|h)'查找.c.h归档。您的regexp语法错误,您使用方括号而不是括号。如果使用方括号,则为[ch]

您也可以只使用默认的regexp类型:find . -regex '.*\.\(c\|h\)$'也可以。请注意,您必须逃脱(|)在这种情况下字符(sedregextype以及)。您不必使用时逃避它们egrepposix-egrepposix-extended


alk*_*alk 3

为什么不直接做:

find ./ -name "*.[c|h]" | xargs grep -n --color func_foo
Run Code Online (Sandbox Code Playgroud)

find ./ -type f -name "*.c" | xargs grep -n --color func_foo
Run Code Online (Sandbox Code Playgroud)

find关于选项的有效参数,-regextype逐字来自man find

-regextype 类型

更改稍后在命令行上发生的 -regex 和 -iregex 测试所理解的正则表达式语法。当前实现的类型有 emacs(这是默认值)、posix-awk、posix-basic、posix-egrep 和 posix-extended

没有类型sed

  • “find -regextype help”给出一个错误,将 sed 列为一种类型...具体来说“find:未知的正则表达式类型 'help';有效类型为 'findutils-default'、'ed'、'emacs'、'gnu- awk'、'grep'、'posix-awk'、'awk'、'posix-basic'、'posix-egrep'、'egrep'、'posix-扩展'、'posix-minimal-basic'、'sed' ”。 (2认同)