https://www.cnblogs.com/peida/archive/2012/12/17/2821195.html
grep [options] -a --text #不要忽略二进制的数据。
-A<显示行数> --after-context=<显示行数> #显示被模式匹配的行及其后#行
-b --byte-offset #在显示符合样式的那一行之前,标示出该行第一个字符的编号。
-B<显示行数> --before-context=<显示行数> #显示被模式匹配的行及其前#行。
-c --count #只输出匹配行的计数。
-C<显示行数> --context=<显示行数>或-<显示行数> #显示被模式匹配的行及其前#行
-d <动作> --directories=<动作> #当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
-e<范本样式> --regexp=<范本样式> #指定字符串做为查找文件内容的样式。
-E --extended-regexp #支持使用扩展的正则表达式
-f<规则文件> --file=<规则文件> #指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
-F --fixed-regexp #将样式视为固定字符串的列表。
-G --basic-regexp #将样式视为普通的表示法来使用。
-h --no-filename #在显示符合样式的那一行之前,不标示该行所属的文件名称。
-H --with-filename #在显示符合样式的那一行之前,表示该行所属的文件名称。
-i --ignore-case #忽略字符大小写的差别。
-l --file-with-matches #列出文件内容符合指定的样式的文件名称。
-L --files-without-match #列出文件内容不符合指定的样式的文件名称。
-o, --only-matching #仅显示匹配到的字符串本身
-n --line-number #在显示符合样式的那一行之前,标示出该行的列数编号。
-q --quiet或--silent #不显示任何信息。
-r --recursive #此参数的效果和指定“-d recurse”参数相同。
-s --no-messages #不显示错误信息。
-v --revert-match #显示不包含匹配文本的所有行。
-V --version #显示版本信息。
-w --word-regexp #只显示全字符合的列。
-x --line-regexp #只显示全列符合的列。
-y #此参数的效果和指定“-i”参数相同。
pattern正则表达式主要参数: \: 忽略正则表达式中特殊字符的原有含义。 ^:匹配正则表达式的开始行。 $: 匹配正则表达式的结束行。 \<:从匹配正则表达 式的行开始。 \>:到匹配正则表达式的行结束。 [ ]:单个字符,如[A]即A符合要求 。 [ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。 .:所有的单个字符。 * :有字符,长度可以为0
注:使用grep匹配时需使用双引号引起来(单引号为强引用),防止被系统误认为参数或者特殊命令而报错。
实例1:排除后面字符串oldboy的内容,使用-v参数输出不包含指定模式的行
grep -v 'oldboy' oldboy.txt
实例2:查找指定进程
命令:
ps -ef|grep svn
输出:
[root@localhost ~]# ps -ef|grep svn
root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
[root@localhost ~]#
说明:
第一条记录是查找出的进程;第二条结果是grep进程本身,并非真正要找的进程。
实例3:查找指定进程个数
命令:
ps -ef|grep svn -c
ps -ef|grep -c svn
输出:
[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn
2
[root@localhost ~]#
实例4:从文件中读取关键词进行搜索
命令:
cat test.txt | grep -f test2.txt
输出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行
实例5:从文件中读取关键词进行搜索 且显示行号
命令:
cat test.txt | grep -nf test2.txt
输出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
说明:
输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号
实例6:从文件中查找关键词
命令:
grep 'linux' test.txt
输出:
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
说明:
实例7:从多个文件中查找关键词
命令:
grep 'linux' test.txt test2.txt
输出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
说明:
多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符
实例8:grep不显示本身进程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
输出:
[root@localhost test]# ps aux|grep ssh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
说明:
实例9:找出已u开头的行内容
命令:
cat test.txt |grep ^u
输出:
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
说明:
实例10:输出非u开头的行内容
命令:
cat test.txt |grep ^[^u]
输出:
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#
说明:
实例11:输出以hat结尾的行内容
命令:
cat test.txt |grep hat$
输出:
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
说明:
实例11:
命令:
输出:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]#
说明:
实例13:显示包含ed或者at字符的内容行
命令:
cat test.txt |grep -E "ed|at"
输出:
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
说明:
实例14:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
命令:
grep '[a-z]\{7\}' *.txt
输出:
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test]#
实例15:用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,在关键字所在行的前两行与后三行也一起捉出来显示
[root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth'
245-PCI: setting IRQ 10 as level-triggered
246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ...
247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10
248:eth0: Identified 8139 chip type 'RTL-8139C'
249-input: PC Speaker as /class/input/input2
250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ...
251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66)
# 如上所示,你会发现关键字 247 所在的前两行及 248 后三行也都被显示出来!
# 这样可以让你将关键字前后数据捉出来进行分析啦!grep 可以使用 --color=auto 来将关键字部分使用颜色显示。
#grep -C 4 "games" /etc/passwd #除了显示符合样式的那一行之外,并显示该行之前后各4行的内容
实例16:根据文件内容递归查找目录
# grep ‘energywise’ * #在当前目录搜索带'energywise'行的文件 # grep -r ‘energywise’ * #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ * #在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件
这几个命令很使用,是查找文件的利器。
实例17:使用-l参数列出包含指定模式的文件的文件名。
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
/etc/passwd
/etc/shadow
root@Linux-world:~#
实例18:
使用 -r 参数递归地查找特定模式
root@Linux-world:~# grep -r linuxtechi /etc/
/etc/subuid:linuxtechi:100000:65536
/etc/group:adm:x:4:syslog,linuxtechi
/etc/group:cdrom:x:24:linuxtechi
/etc/group:sudo:x:27:linuxtechi
/etc/group:dip:x:30:linuxtechi
/etc/group:plugdev:x:46:linuxtechi
/etc/group:lpadmin:x:115:linuxtechi
/etc/group:linuxtechi:x:1000:
/etc/group:sambashare:x:131:linuxtechi
/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
............................................................................
实例19:使用
-i参数在查找时忽略字符的大小写。
nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd
linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
nextstep4it@localhost:~$
实例20:
使用 -e 参数查找多个模式
root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
#在一条grep命令中查找‘linuxtechi’和‘root’单词,使用-e参数
实例21:使用 -f 用文件指定待查找的模式
首先,在当前目录中创建一个搜索模式文件“grep_pattern”,我想文件中输入的如下内容。
root@Linux-world:~# cat grep_pattern
^linuxtechi
root
false$
root@Linux-world:~#
现在,试试使用grep_pattern文件进行搜索
root@Linux-world:~# grep -f grep_pattern /etc/passwd
实例22:只匹配整个单词,而不是字符串的一部分
grep -w pattern files //(如匹配‘magic’,而不是‘magical’),
实例23:使用正则匹配更复杂的检测条件
zhouwj@zhouwj-virtual-machine:~$ echo this is a test line. | grep -o -E "[a-z]+\."
输出:
line.
$grep -E -o "'ip': '[0-9\.]+?'"
输出:
'ip': '64.134.243.119'
'ip': '218.197.31.248'
.................
扩展grep(grep -E 或者 egrep):
使用扩展grep的主要好处是增加了额外的正则表达式元字符集。
打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出。
# egrep 'NW|EA' testfile northwest NW Charles Main 3.0 .98 3 34 eastern EA TB Savage 4.4 .84 5 20
对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。
#grep 'NW\|EA' testfile northwest NW Charles Main 3.0 .98 3 34 eastern EA TB Savage 4.4 .84 5 20
搜索所有包含一个或多个3的行。
# egrep '3+' testfile # grep -E '3+' testfile # grep '3\+' testfile #这3条命令将会 northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 northeast NE AM Main Jr. 5.1 .94 3 13 central CT Ann Stephens 5.7 .94 5 13
搜索所有包含0个或1个小数点字符的行。
# egrep '2\.?[0-9]' testfile # grep -E '2\.?[0-9]' testfile # grep '2\.\?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 eastern EA TB Savage 4.4 .84 5 20
搜索一个或者多个连续的no的行。
# egrep '(no)+' testfile # grep -E '(no)+' testfile # grep '\(no\)\+' testfile #3个命令返回相同结果, northwest NW Charles Main 3.0 .98 3 34 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9
1、或操作
grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep同样可以实现 awk '/123|abc/' filename // awk 的实现方式
2、与操作
grep pattern1 files | grep pattern2 //显示既匹配 pattern1 又匹配 pattern2 的行。
转载:https://linux.cn/article-5453-1.html
https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html