带有find命令的regextype

问题描述 投票:4回答:2

我试图使用-regextype的find命令,但它无法正常工作。

我试图找到所有ch文件发送到管道和grep名称,这些文件中的func_foo。我错过了什么?

$ find ./ -regextype sed -regex ".*\[c|h]" | xargs grep -n --color func_foo

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

$ find  ./  -type  f  "*.c"  |  xargs  grep  -n  --color  func_foo
regex linux find
2个回答
3
投票

为什么不这样做:

find ./ -name "*.[c|h]" | xargs grep -n --color func_foo

find ./ -type f -name "*.c" | xargs grep -n --color func_foo

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

-regextype类型

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

没有类型sed


4
投票

接受的答案包含一些不准确之处。在我的系统上,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'.

例如。 find . -regextype egrep -regex '.*\.(c|h)'找到.c.h文件。您的正则表达式语法错误,您使用方括号而不是括号。方括号,它将是[ch]

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

© www.soinside.com 2019 - 2024. All rights reserved.