regex - 在 Mac OS X 上使用命令 find 扩展 POSIX 实用程序?

标签 regex macos bash terminal find

我尝试在我的 Mac OS X 10.9 终端上执行此命令:

find / -type f -regextype posix-extended -regex "(.*A.*a.*.*)|(.*a.*A.*.*)" -exec tail -n 3 '{}' \;

但是 Bash v 3.2 的响应是:

find: -regextype: unknown primary or operator

我对终端联机帮助页进行了深入搜索:

man find

man regex

等...

但我一无所获。 Mac OS X Bash v3.2 是否应该更新?似乎选项“-regextype”不包含在“查找”中。

我该如何解决?

附注 该命令在 Ubuntu Linux 14 上完美运行。

最佳答案

对您的问题最简单的回答是,在 OS X 上您需要使用 -E 选项而不是 -regextype posix-extended

即:

find / -type f -regextype posix-extended -regex "(.*A.a..*)|(.*a.A..*)" -exec tail -n 3 '{}' \;

变成:

find -E / -type f -regex "(.*A.a..*)|(.*a.A..*)" -exec tail -n 3 '{}' \;

除了提示的 -exec tail 命令外,上面的命令适用于我的 10.10 安装。

这实际上包含在手册页的第一个条目中:

 The options are as follows:

 -E      Interpret regular expressions followed by -regex and -iregex primaries as extended (modern) regular expressions rather than basic
         regular expressions (BRE's).  The re_format(7) manual page fully describes both formats.

关于regex - 在 Mac OS X 上使用命令 find 扩展 POSIX 实用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094004/

相关文章:

macos - 在 macOS 上,如何找到使用 “open” 命令打开的应用程序路径?

java - 如何在 macOS 上显示空间变化

arrays - 在 Bash 中添加和列出数组元素

Bash - 使用标准输入和 EOF 的 Sudo 身份验证

java - 引用java正则表达式命名组的多个匹配项

正则表达式 : How to remove all whitespaces around certain character?

python - re.sub() 负向后看 + 负向前看

mysql - 查找数据库中所有没有尾部斜杠的链接

objective-c - NSString drawAtPoint 文本未显示

bash - 无法为 Android Studio 安装 ARCore 模拟器