搜索字符串:查找和Grep [英] Search String: Find and Grep

查看:113
本文介绍了搜索字符串:查找和Grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有一个更好/更短的方法:

There must be a better / shorter way to do this:

# Find files that contain <string-to-find> in current directory
#   (including sub directories) 
$ find . | xargs grep <string-to-find>

HTML文件:

 # find . | grep html$ | xargs grep <string-to-find>

先感谢!

推荐答案

find . -name \*.html

或者,如果要查找名称与正则表达式匹配的文件:

or, if you want to find files with names matching a regular expression:

find . -regex filename-regex.\*\.html 

或者,如果要搜索名称与正则表达式匹配的文件中的正则表达式

or, if you want to search for a regular expression in files with names matching a regular expression

find . -regex filename-regex.\*\.html -exec grep -H string-to-find {} \;

grep 参数 -H 输出文件的名称,如果感兴趣。如果没有,你可以安全地删除它,只需使用 grep 。这将指示找到为它找到的每个文件名执行 grep string-to-find filename 的参数列表太长,需要 find 完成执行,然后才能将结果传递给 xargs

The grep argument -H outputs the name of the file, if that's of interest. If not, you can safely remove it and simply use grep. This will instruct find to execute grep string-to-find filename for each file name it finds, thus avoiding the possibility of the list of arguments being too long, and the need for find to finish executing before it can pass its results to xargs.

要处理您的示例:

find . | xargs grep <string-to-find>

可以替换为

find . -exec grep -H string-to-find {} \;

find . | grep html$ | xargs grep <string-to-find>

可以替换为

find . -name \*.html -exec grep -H string-to-find {} \;

这篇关于搜索字符串:查找和Grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆