shell之grep操作
GREP
grep(Global search Regular Expression and Print out the line)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
grep 例子
grep 'word' filename
: 搜索文件中包含word的行;grep -i 'bar' file1
:在文件中不区分大小写搜索bar;grep -R foo
: 在当前文件夹及其子文件下搜索单词foo;grep -c 'nixcraft' frontpage.md
: 在frontpage.md文件中搜索并显示'nixcraftq'出现的次数;
在linux和unix中的语法
grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2' filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName
使用grep在linux文件中搜索
在/etc/passwd文件中搜索boo:
grep boo /etc/passwd
不区分大小写的搜索boo,Boo和BOO等:
grep -i "boo" /etc/passwd
最后grep命令可以接在cat之后:
cat /etc/passwd | grep -i "boo"
迭代使用
-R或-r参数可以在文件夹和子文件夹下迭代查找
grep -r "192.168.1.5" /etc/
返回为
/etc/ppp/options:# ms-wins 192.168.1.50
/etc/ppp/options:# ms-wins 192.168.1.51
-h参数可以略去文件名,-n参数可以显示行号:
显示前后行的信息
如果希望显示显示前后行的信息,可以使用 -C n
表示显示匹配行的前后n行
grep -C 3 'boo' file
仅搜索单个单词
当使用grep搜索boo时,返回的可能有fooboo,boo123等。-w参数可以仅搜索一个boo单词:
grep -w "boo" file
搜索两个不同的单词
使用egrep命名:
egrep -w 'word1|word2' file
显示文件中被匹配的行数
使用-c参数,grep可以报告每个文件中匹配的行数
grep -c 'word' /path/to/file
显示未匹配
使用-v参数可以打印未匹配的行:
grep -v 'bar' /path/to/file
linux管道和grep
grep参数可以用在pipes中,使用|
命令。
例如打印cpu模式
cat /proc/cpuinfo | grep -i 'Model'
可以结合apt search
寻找安装包
列出匹配成功的文件名
使用-l命令列出包含main()的文件名:
grep -l 'main' *.c
总结
参数 | 描述 |
---|---|
-i | 忽略大小写 |
-n | 仅匹配整个单词 |
-v | 选择未匹配模式 |
-n | 打印行号 |
-h | 不显示文件名 |
-r | 迭代进行 |
-R | 迭代进行 |
-l | 仅打印匹配成功的文件名 |
-c | 仅打印文件中匹配成功的行数 |
-C n | 显示匹配行的前后n行 |
--color | 使用彩色显示成功匹配 |
grep中正则表达式
grep中正则表达式示例
例如在/etc/passwd中搜索'vivek', grep 'vivek' /etc/passwd'
不区分大小写以单词形式,grep -iw 'vivek' /etc/passwd'
搜索vivek或raj,使用扩展表达式 grep -iEw 'vivek|raj' /etc/passwd
,
最后一种情况也可以使用egrep, egrep -i '^(linux|unix)' filename
egrep相当于grep -E
。
匹配单个字母
在正则表达式中.
匹配任何单个字母。
为仅匹配.
可以使用转义符号\
,即grep "\." filename
锚点
可以使用^
或$
将匹配位置限制在开头或结尾
grep -w '^username' /etc/passwd
grep 'foo 'foo$' filename
匹配一行仅有foo: grep '^foo&' filename
匹配空行:grep '^$' fileaname
使用grep匹配字母集合
可以使用[..]
匹配特定字母和字母范围,例如想匹配ViVek
或vivek
grep '[vV]ivek' filename
grep '[vV][iI][Vv][Ee][kK]' filename
也可以匹配带有数字的,例如vivek1或Vivek2:grep '[vV]ivek[0-9]' filename
匹配多个数字:grep 'foo[0-9][0-9]' filename
匹配至少一个字母:grep [A-Za-z] filename
匹配包含字母w或n:grep [wn] filename
在方括号表达式中,在"[:"和":]"之间的名称表示了标准的字母类别:
名称 | 描述 |
---|---|
[[:alnum:]] |
字母数字 |
[[:alpha:]] |
字母 |
[[:blank:]] |
空格或tab |
[[:digit:]] |
数字 |
[[:lower:]] |
小写字母 |
[[:upper:]] |
大写字母 |
[[:space:]] |
空格字符:制表符、换行符、垂直制表符、换行符、回车符和空格 |
grep '[[:upper:]] filename'
正则表达中的逆匹配^
在正则表达式中使用^
表示不匹配:grep '[vV]ivek[^0-9]]' test
通配符 .
符号.
匹配任意字母,在下面的例子中匹配三个字母的词:grep '\<b.t\>' word
其中:
\<
匹配单词开头的空字符串\>
匹配单词结尾的空字符串
使用\
转义.
当想匹配一个地址时,grep '192.168.1.254' /etc/host
可能会得到192.168.2.18 和192x168y2z18
下面的代码匹配地址:
egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' filename
-e
匹配以-
开头的模式
当匹配--test--
模式时,需要加上-e
参数,否则会将--test--
作为参数。
\|
或操作
当想获得OR操作时,有三中操作:
grep -E 'word1|word2' filename
egrep 'word1|word2' filename
grep 'word1\|word2' filename
AND操作
当想在匹配行中同时存在word1和word2时,有两种操作
grep 'word1' filename | grep 'word2'
grep 'word1.*word2|word2.*word1' filename
{}
控制匹配次数
可以使用{}
控制匹配次数
{N}
{N,}
{min, max}
例如匹配v两次: egrep 'v{2}' filename
;
匹配col和cool: egrep 'co{1,2}l' filename
;
匹配c至少三次: egrep 'c{3,}' filename
;
总结
操作 | 描述 | 例子 |
---|---|---|
. | 任意单个字符 | grep '.' file , grep 'foo.' input |
? | 对应字符可选,最多匹配一次 | grep 'vivek?' /etc/passwd |
* | 对应项匹配0次或多次 | grep 'vivek*' /etc/passwd |
+ | 对应项匹配1次或多次 | `ls /var/log/ |
对应项匹配N次 | egrep '[0-9]{2} input |
|
对应项至少匹配N次 | egrep '[0-9]{2,} input |
|
对应项匹配N到M次 | egrep '[0-9]{2,4} input |
|
– | 不出现在结尾或开头表示范围 | grep ':/bin/[a-z]*' /etc/passwd |
^ | 出现在开头表示空白字符串,出现在范围为不匹配项 | grep '^vivek' /etc/passwd ,grep '[^0-9]*' /etc/passwd |
$ | 出现记为表示空白字符串 | grep '^$' /etc/passwd |
\b | 匹配字符串边缘的空白 | grep '\bvivek' /etc/passwd |
\B | 匹配空字符串,前提是它不在单词的边缘 | grep '\B/bin/bash /etc/passwd |
< | 匹配单词前的空字符串 | grep '\<d' filename |
> | 匹配单词后的空字符串 | grep 'bash\>' /etc/passwd ,grep '\' /etc/passwd |
参考
https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
https://www.cyberciti.biz/faq/grep-regular-expressions/