用‘grep’搜索文本文件
如果您要在几个文本文件中查找一字符串,可以使用‘grep’命令。‘grep’在文本中搜索指定的字符串。
举个例子:假设您正在‘/usr/src/linux/Documentation’目录下搜索带字符串‘magic’的文件:
$ grep magic /usr/src/linux/Documentation/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
其中文件‘sysrp.txt’包含该字符串,讨论的是 SysRQ 的功能。
默认情况下,‘grep’只搜索当前目录。如果此目录下有许多子目录,‘grep’会以如下形式列出:
grep: sound: Is a directory
这可能会使‘grep’的输出难于阅读。这里有两种解决的办法:
明确要求搜索子目录:grep -r
忽略子目录:grep -d skip
当然,如果
预料到有许多输出,您可以通过
管道
将其转到‘less’上阅读:
$ grep magic /usr/src/linux/Documentation/* | less
这样,您就可以更方便地阅读。
有一点要注意,您必需提供一个文件过滤方式(搜索全部文件的话用*)。如果您忘了,‘grep’会一直等着,直到该程序被中断。如果您遇到了这样的情况,按
ctrl+c,然后再试。
(important!!)---------------------------------------------------
cat 连续显示、查看文件内容
more 分页查看文件内容
less 分页可控制查看文件内容
通俗点说:
cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式操作,适合察看内容短小、不超过一屏的文件;
more比cat强大一点,支持分页显示,你可以ctrl+B ctrl+F .....上下滚屏,但是不支持像shift+G(跳到文件尾)这种操作;
less比more更强大一点,支持各种命令,随便翻页、跳转、查找.....想怎么看,就怎么看,爱怎么看,就怎么看。想编辑时直接v 就可以编辑
---------------------------------------------------
下面是一些有意思的
命令行参数:
grep
-i pattern files:不区分大小写地搜索。默认情况区分大小写,
grep
-l pattern files :只列出匹配的文件名,
grep
-L pattern files :列出不匹配的文件名,
grep
-w pattern files:只匹配整个单词,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’),
grep
-C number pattern files:匹配的上下文分别显示[number]行,
grep
pattern1 | pattern2 files :显示匹配 pattern1 或 pattern2的行,
(Not correct)
grep pattern1 files
| grep pattern2 :显示既匹配 pattern1 又匹配pattern2 的行。
The --only-matching option (also -o) changes grep's behavior. so that it outputs not the entire lines containing a match to the regexp but only those matches themselves.
grep -o 'en' video.txt-----------------------------------------------------------------------------------------------------
1. Grep OR Using \|
If you use the grep command without any option, you need to use \| to separate multiple patterns for the or condition.
grep 'pattern1\|pattern2' filename
2. Grep OR Using -E
grep -E option is for extended regexp. If you use the grep command
with -E option, you just need to use | to separate multiple patterns for
the or condition.
grep -E 'pattern1|pattern2' filename
3. Grep OR Using egrep
egrep 'pattern1|pattern2' filename
4. Grep OR Using grep -e
Using grep -e option you can pass only one parameter. Use multiple -e
option in a single command to use multiple patterns for the or
condition.
grep -e pattern1 -e pattern2 filename
5. Grep AND using -E ‘pattern1.*pattern2′
There is no AND operator in grep. But, you can simulate AND using grep -E option.
grep -E 'pattern1.*pattern2' filename
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
6. Grep AND using Multiple grep command
You can also use multiple grep command separated by pipe to simulate AND scenario.
grep -E 'pattern1' filename | grep -E 'pattern2'
The following example will grep all the lines that contain both “Manager” and “Sales” in the same line.
$ grep Manager employee.txt | grep Sales
7. Grep NOT using grep -v
Using grep -v you can simulate the NOT conditions. -v option is for
invert match. i.e It matches all the lines except the given pattern.
grep -v 'pattern1' filename
这里还有些
用于搜索的特殊符号:
\< 和 \> 分别标注单词的开始与结尾。
例如:
grep man * 会匹配 ‘Batman’、‘manic’、‘man’等,
grep '\
grep '\' 只匹配‘man’,而不是‘Batman’或‘manic’等其他的字符串。
'^':指匹配的字符串在行首,
'$':指匹配的字符串在行尾,
如果您不习惯命令行参数,可以试试图形界面的‘grep’,如 reXgrep 。这个软件提供 AND、OR、NOT 等语法,还有漂亮的按钮 :-) 。如果您只是需要更清楚的输出,不妨试试 fungrep 。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7899089/viewspace-734995/
,如需转载,请注明出处,否则将追究法律责任。