使用 ACK 作为过滤器
我一直在使用 ACK 来搜索我的代码库,它是一个很棒的工具。然而,(在我看来)它有一个重要的限制——它不允许正则表达式的多行匹配。
为了克服这个限制,我想过滤一组包含特定表达式的文件,然后再次过滤它们以查找第二个表达式(假设两个表达式很可能不在同一行)。我尝试运行以下命令,但没有成功(它什么也不返回):
ack -l --type=java "(List|Collection|Map|Set)" | ack --type=java "String"
而且我不想使用 grep,因为我想将搜索限制为 java 文件,忽略 .cvs 目录, .svn 目录等(ack
默认情况下执行的操作)有什么想法吗?
I've been using ACK for searching my code base, and it's a wonderful tool. However, it has (in my opinion) one important limitation - it does not allow multi-line matching of regular expressions.
To overcome that limitation, I'd like to filter a set of files which contain a certain expression, and then filter them again looking for a second expression (given that both expressions are most likely not on the same line). I've tried running the following command, with no success (it returns nothing):
ack -l --type=java "(List|Collection|Map|Set)" | ack --type=java "String"
And I'd rather not use grep
, since I want to limit my searches to java files, ignoring .cvs dirs, .svn dirs, etc. (something ack
does by default) Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的命令行正在文件列表上执行 ack,而不是使用匹配文件列表作为另一组要检查的文件。也就是说,它与执行 xargs 相同
,是您想要用来将第一个命令的输出视为可以运行第二个 ack 调用的一组文件的工具。
--print0
用空字符分隔文件名,以确保 xargs 获取正确的文件名(以防其中有奇数字符)。 xargs 的-0
标志告诉它期望文件名以 null 分隔。Your current command line is executing ack on the list of files, not using the list of matched files as another set of files to inspect. That is, it'd be the same as doing
xargs
is the tool you want to use to treat the output from the first command as a set of files with which the second ack invocation can run.The
--print0
separates the filenames by null characters to ensure that xargs gets the correct filename (in case there are odd characters in it). The-0
flag to xargs tells it to expect the filenames to be null separated.