[英]awk, sed, grep specific strings from a file in Linux
[英]grep recursively for a specific file type on Linux
我们可以仅在特定文件(html 文件)中的某些文件夹中递归搜索术语(例如“onblur”)吗?
grep -Rin "onblur" *.html
这不返回任何内容。 但,
grep -Rin "onblur" .
从所有可用文件中返回"onblur"
搜索结果,例如 text(".txt")、.mako、.jinja 等。
这也可能对您有所帮助:以递归方式 grep 某些文件类型 | commandlinefu.com 。
命令是:
grep -r --include="*.[ch]" pattern .
在你的情况下是:
grep -r --include="*.html" "onblur" .
grep -r --include "*.html" onblur .
来自: 我如何递归地grep?
您可能还喜欢 ag 'the silver searcher' -
ag --html onblur
它通过正则表达式搜索,默认情况下在当前目录中递归,并具有预定义的搜索扩展集 - 在这种情况下 --html 映射到 .htm、.html、.shtml、.xhtml。 默认情况下还会忽略二进制文件、打印文件名、行号并为输出着色。
一些选项 -
-Q --literal
Do not parse PATTERN as a regular expression. Try to match it literally.
-S --smart-case
Match case-sensitively if there are any uppercase letters in PATTERN,
case-insensitively otherwise. Enabled by default.
-t --all-text
Search all text files. This doesn't include hidden files.
--hidden
Search hidden files. This option obeys ignored files.
要查看支持的文件类型列表,请运行ag --list-file-types
。
它似乎唯一缺乏的是能够指定带有扩展名的文件类型,在这种情况下,您需要使用 --include 来使用 grep。
看看这个答案,一个类似的问题: grep,但只有某些文件扩展名
这对我有用。 在您的情况下,只需键入以下内容:
grep -inr "onblur" --include \*.html ./
考虑到
grep
:命令
-r
: 递归
-i
: 忽略大小写
-n
: 每个输出行前面都有它在文件中的相对行号
--include \\*.html
: 用 \\ 转义,以防万一您的目录中文件名带有星号
./
: 从当前目录开始。
通过键入grepy mystring .
只能从 .py 文件进行 grep grepy mystring .
我将以下行添加到我的 bashrc:
alias grepy='grep -r --include="*.py"'
另请注意,grep 接受以下内容:
grep mystring *.html
用于当前文件夹中的 .html 搜索
grep mystring */*.html
用于递归搜索(不包括当前目录中的任何文件!)。
grep mystring .*/*/*.html
用于递归搜索(当前目录中的所有文件和子目录中的所有文件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.