不看绝对后悔的Linux三剑客之grep实战精讲

原文:http://blog.51cto.com/hujiangtao/1923675

https://www.cnblogs.com/peida/archive/2012/12/17/2821195.html

三、Linux三剑客之grep命令精讲

[命令简介]
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

[功能说明]
grep***** ==擅长过滤器,把想要的或者不想要的分离开。Linux三剑客 老三。

[用法格式]
grep [选项]... PATTERN [FILE]...

[参数选项]
[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   #列出文件内容不符合指定的样式的文件名称。

-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”参数相同。

#Context control:
-B 除了显示匹配的一行之外,并显示该行之前的num行
-A 除了显示匹配的一行之外,并显示该行之后的num行
-C 除了显示匹配的一行之外,并显示该行之前后各num行

grep "String" -B 10 test.txt #显示匹配的String行和String的前10行。

规则表达式:

grep的规则表达式:

^  #锚定行的开始 如:'^grep'匹配所有以grep开头的行。

$  #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。

.  #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。

*  #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。

.*   #一起用代表任意字符。

[]   #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。

[^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。

\(..\)  #标记匹配字符,如'\(love\)',love被标记为1。

\<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。

\>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。

x\{m\}  #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。

x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。

x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。

\w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。

\W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。

\b    #单词锁定符,如: '\bgrep\b'只匹配grep。

POSIX字符:

为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:]是[A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。

[:alnum:]    #文字数字字符

[:alpha:]    #文字字符

[:digit:]    #数字字符

[:graph:]    #非空字符(非空格、控制字符)

[:lower:]    #小写字符

[:cntrl:]    #控制字符

[:print:]    #非空字符(包括空格)

[:punct:]    #标点符号

[:space:]    #所有空白字符(新行,空格,制表符)

[:upper:]    #大写字符

[:xdigit:]   #十六进制数字(0-9,a-f,A-F)

[实践案例]

实战准备:
1、调整别名
alias grep='grep --color=auto'
 
注意字符集:可能带来的问题
export LC_ALL=C

1、查找指定进程:

[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进程本身,并非真正要找的进程。

2、查找指定进程个数:

[root@localhost ~]# ps -ef|grep svn -c
 2
 [root@localhost ~]# ps -ef|grep -c sshd
 6
 [root@localhost ~]#

#匹配进程输出多少行的计数。这里表示sshd输出有6行。

3、从文件中读取关键词进行搜索:

[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文件中读取出的关键词的内容行。

4、从文件中读取关键词进行搜索 且显示行号:

[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文件中读取出的关键词的内容行,并显示输出每一行的行号。

5、从文件中查找关键词 并显示行号:

[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]#

#显示匹配字符串’linux’的行,并且显示输出行的行号。

6、从多个文件中查找关键词:

[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]#

#多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符

7、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   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

#ps -aux|grep [s]sh这句命令意思笔者也不是很清楚,但是能实现效果;ps aux | grep ssh 输出结果继续交给管道后面的grep -v "grep"命令处理,-v过滤掉了 grep 本身进程。

8、找出以u开头的行内容:

[root@localhost test]# cat test.txt |grep ^u
 ubuntu
 ubuntu linux
 [root@localhost test]#

#使用正则表达式“ ^ ”匹配以u字母的开始行;“ ^ ”放在要匹配的字符串前。

9、输出非u开头的行内容:

[root@localhost test]# cat test.txt |grep ^[^u]
 hnlinux
 peida.cnblogs.com
 redhat
 Redhat
 linuxmint
 [root@localhost test]#

10、输出以hat结尾的行内容:

[root@localhost test]# cat test.txt |grep hat$
 redhat
 Redhat
 [root@localhost test]#

#使用正则表达式“ $ ”匹配以hat字符串为结尾的行;“ $ ”放在要匹配的字符串后。

11、ifconfig匹配过滤出ip地址:

[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]#

#“[0-9]\{1,3\}\.”表示“.”符号前面重复0-9中的数字,至少1个,不多于或等于3个。“\”转义符,把原本的意义(马甲)去掉,比如“\{1,3\}”把“{ 和 }”符号默认的意义转变成其它意义。

# "([0-9]{1,3}\.){3}[0-9]" 表示匹配包含3个 ([0-9]{1,3}\.) 字符串的行,并且后面匹配有[0-9]中的数字。

12、显示包含ed或者at字符的内容行:

[root@localhost test]# cat test.txt |grep -E "peida|com"
 peida.cnblogs.com
 [root@localhost test]# cat test.txt |egrep "ed|at"
 redhat
 Redhat
 [root@localhost test]#

#使用-E匹配(过滤)出多个字符串,用“|”符号隔开字符串。
#‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。使用egrep等于使用grep -E。

13、显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行:

[root@localhost test]# grep '[a-z]\{7\}' *.txt
 test.txt:hnlinux
 test.txt:peida.cnblogs.com
 test.txt:linuxmint
 [root@localhost test]#

#重复匹配所有小写字母7次的行。默认是区分大小写的,所以用小写字母匹配就不会匹配到大写。-i可以解除区分大小写的限制。

14、上下文控制Context control参数选项的使用:

[root@oldboy66-23 ~]# seq 100 >test.txt       
 [root@oldboy66-23 ~]# grep "20" -A 3 test.txt 
 20
 21
 22
 23
 [root@oldboy66-23 ~]# grep "20" -B 3 test.txt 
 17
 18
 19
 20
 [root@oldboy66-23 ~]# grep "20" -C 2 test.txt 
 18
 19
 20
 21
 22

Context control上下文控制参数小结:
-B 除了显示匹配的一行之外,并显示该行之前的num行
-A 除了显示匹配的一行之外,并显示该行之后的num行
-C 除了显示匹配的一行之外,并显示该行之前后各num行
使用格式: grep "String" -B 10 test.txt

15、正则表达式案例一:

案例文件内容:
 [root@oldboy oldboy]# cat oldboy.log
 I am oldboy teacher!
 I teach linux.
 
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  
 our site is http://www.etiantian.org  
 my qq num is 49000448.
 
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
============================================
实战举例:
1)^word  搜索以word开头的。vi  ^一行的开头
2)word$  搜索以word结尾的。vi  $一行的末尾
3)^$     表示空行,能理解么?
============================================
a.过滤出来以m开头的行
 [root@oldboy log]# grep "^m" oldboy.log 
 my blog is http://oldboy.blog.51cto.com  my qq num is 49000448.
 my god ,i am not oldbey,but OLDBOY!
 
b.过滤出来以m结尾的行
 [root@oldboy log]# grep "m$" oldboy.log  
 my blog is http://oldboy.blog.51cto.com  [root@oldboy log]# cat -n oldboy.log 
     1  I am oldboy teacher!
     2  I teach linux.
     3
     4  I like badminton ball ,billiard ball and chinese chess!
     5  my blog is http://oldboy.blog.51cto.com      
     6  our site is http://www.etiantian.org      
     7  my qq num is 49000448.
     8
     9  not 4900000448.
    10  my god ,i am not oldbey,but OLDBOY!
c.过滤掉空行
 [root@oldboy log]# grep -v "^$" oldboy.log 
 I am oldboy teacher!
 I teach linux.
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  
 our site is http://www.etiantian.org  
 my qq num is 49000448.
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 [root@oldboy log]# grep -vn "^$" oldboy.log  #过滤掉空行且显示行号
 1:I am oldboy teacher!
 2:I teach linux.
 4:I like badminton ball ,billiard ball and chinese chess!
 5:my blog is http://oldboy.blog.51cto.com  
 6:our site is http://www.etiantian.org  
 7:my qq num is 4900044
 8.
 9:not 4900000448.
 10:my god ,i am not oldbey,but OLDBOY!

16、正则表达式案例二:

实战举例:
4).  代表且只能代表任意一个字符。
5)\  例 \. 就只代表点本身,转义符号,让有着特殊身份意义的字符,脱掉马甲,还原原形。\$。
6)*  例 s* 重复0个或多个前面的一个字符
7).*  匹配所有字符。延伸 ^.* 以任意多个字符开头。 .*$ 以任意多个字符结尾
=============================================
a.匹配任意一个字符
 [root@oldboy log]# grep "." oldboy.log       
 I am oldboy teacher!
 I teach linux.
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 #没有空行?因为“.”代表任意一个字符,空行没有字符,所以匹配不到。
 
b.匹配以点为结尾的(错误方法)
 [root@oldboy log]# grep ".$" oldboy.log  #系统把“.”识别成了正则表达式的字符
 I am oldboy teacher!
 I teach linux.
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 
c.只匹配点, \ 转义。(正确方法)
 [root@oldboy log]# grep "\.$" oldboy.log
 I teach linux.
 my qq num is 49000448.
 not 4900000448.
 #使用转义符“\”让有着特殊身份意义的字符,脱掉马甲,还原原形。
d.“*”的例子,及-o精确匹配输出。
 [root@oldboy log]# grep "0*" oldboy.log   #使用“*”不会过滤掉原来的内容
 I am oldboy teacher!
 I teach linux.
 
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 
 [root@oldboy log]# grep -o "0*" oldboy.log 
 000
 00000
 
=============================================
e.“.*”的匹配。
 [root@oldboy log]# grep ".*" oldboy.log  #匹配所有字符,所以全部都输出了
 I am oldboy teacher!
 I teach linux.
 
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 [root@oldboy log]# grep "^.*" oldboy.log   #匹配所有字符任意长度开头
 I am oldboy teacher!
 I teach linux.
 
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 [root@oldboy log]# grep ".*$" oldboy.log   #匹配所有字符任意长度结尾
 I am oldboy teacher!
 I teach linux.
 
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 
f.“.”的深入匹配。
 [root@oldboy-test ~]# echo 'oldb y' >>oldboy.log
 [root@oldboy-test ~]# tail -1 oldboy.log
 oldb y
 [root@oldboy log]# grep "oldb.y" oldboy.log    #匹配oldb“任意字符”y
 I am oldboy teacher!
 my blog is http://oldboy.blog.51cto.com  my god ,i am not oldbey,but OLDBOY!
 oldb y
 [root@oldboy log]# grep -o "oldb.y" oldboy.log   #匹配oldb“任意字符”y
 oldboy
 oldboy
 oldbey
 oldb y
 #空格也算是字符,所以oldb y被匹配出来了。注意,但是空行没有字符。

17、正则表达式案例三:

实战举例:
8)[abc]  匹配字符集合内的任意一个字符[a-zA-Z],[0-9]。
9)[^abc] 匹配不包含^后的任意字符的内容。中括号里的^为取反,注意和以..开头区别。
10)a\{n,m\} 重复n到m次,前一个重复的字符。如果用egrep/sed -r可以去掉转义符。
11)\{n,\}  重复至少n次,前一个重复的字符。如果用egrep/sed -r可以去掉转义符。
12)\{n\}  重复n次,前一个重复的字符。如果用egrep/sed -r可以去掉转义符。
13)\{,m\} ??????   #“\{,m\}”按照man grep的说明来测试,没成功
注意:egrep,grep -E或sed -r过滤一般特殊字符可以不转义。
=============================================
a.过滤出ifconfig eth0的IP行。
[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]#

#“[0-9]\{1,3\}\.”表示“.”符号前面重复0-9中的数字,至少1个,不多于或等于3个。“\”转义符,把原本的意义(马甲)去掉,比如“\{1,3\}”把“{ 和 }”符号默认的意义转变成其它意义。

# "([0-9]{1,3}\.){3}[0-9]" 表示匹配包含3个 ([0-9]{1,3}\.) 字符串的行,并且后面匹配有[0-9]中的数字

小结:
grep一般常用参数:
“*”星越多表示越重要!五星最重要!


-a:在二进制文件中,以文本文件的方式搜索数据

没加-a前,匹配二进制文件会提示:
 [root@localhost ~]# grep 1 /bin/cp
 Binary file /bin/cp matches
 加-a后,就可以匹配二进制文件了。但是匹配后会产生乱码,所以这里就不截图了。

#-a的乱码解决方法可以退出重新进入 或者setup然后退出。

-c:计算找到 ’搜索字符串’ 的次数

[root@localhost ~]# ps -ef|grep svn -c
 2
 [root@localhost ~]# ps -ef|grep -c sshd
 6
 [root@localhost ~]#

-o:仅显示出匹配regexp的内容

[root@oldboy oldboy]# cat oldboy.log
 I am oldboy teacher!
 I teach linux.
 
 I like badminton ball ,billiard ball and chinese chess!
 my blog is http://oldboy.blog.51cto.com  our site is http://www.etiantian.org  my qq num is 49000448.
 
 not 4900000448.
 my god ,i am not oldbey,but OLDBOY!
 [root@oldboy log]# grep -o "0*" oldboy.log 
 000
 00000

-i*****:忽略大小写的不同,所以大小写视为相同*****

[root@oldboy log]# grep -i "OLDb.y" oldboy.log
 I am oldboy teacher!
 my blog is http://oldboy.blog.51cto.com  my god ,i am not oldbey,but OLDBOY!
 oldb y
 [root@oldboy log]# grep -oi "oLDb.Y" oldboy.log
 oldboy
 oldboy
 oldbey
 oldb y

-n*****:在行首显示匹配内容行的行号*****

[root@localhost test]# grep -n 'linux' test.txt
 1:hnlinux
 4:ubuntu linux
 7:linuxmint
 [root@localhost test]#

-v*****:反向选择,即不显示 ‘搜索字符串’ 内容的那一行*****

[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

-E*****:扩展的grep,即egrep*****

[root@localhost test]# cat test.txt |grep -E "peida|com"
 peida.cnblogs.com
 [root@localhost test]# cat test.txt |egrep "ed|at"
 redhat
 Redhat
 [root@localhost test]#

--color=auto***:以特定颜色高亮显示匹配关键字(不是整行)***

#提示: -i -v 为常用参数。

Context control上下文控制参数:

使用格式: grep "String" -B 10 test.txt

-A:After的意思,显示匹配字符串及其后n行的数据

[root@oldboy66-23 ~]# seq 100 >test.txt       
 [root@oldboy66-23 ~]# grep "20" -A 3 test.txt 
 20
 21
 22
 23

-B:beforce的意思,显示匹配字符串及其前n行的数据

[root@oldboy66-23 ~]# grep "20" -B 3 test.txt 
 17
 18
 19
 20

-C:显示匹配字符串及其前后各num行的数据

[root@oldboy66-23 ~]# grep "20" -C 2 test.txt 
 18
 19
 20
 21
 22

使用实例:

实例1:查找指定进程

命令:

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进程本身,并非真正要找的进程。

实例2:查找指定进程个数

命令:

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 ~]#

说明:

实例3:从文件中读取关键词进行搜索

命令:

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文件中读取出的关键词的内容行

实例3:从文件中读取关键词进行搜索 且显示行号

命令:

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文件中读取出的关键词的内容行,并显示每一行的行号

实例5:从文件中查找关键词

命令:

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]#

说明:

实例6:从多个文件中查找关键词

命令:

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]#

说明:

多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符

实例7: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

说明:

实例8:找出已u开头的行内容

命令:

cat test.txt |grep ^u

输出:

[root@localhost test]# cat test.txt |grep ^u

ubuntu

ubuntu linux

[root@localhost test]#

说明:

实例9:输出非u开头的行内容

命令:

cat test.txt |grep ^[^u]

输出:

[root@localhost test]# cat test.txt |grep ^[^u]

hnlinux

peida.cnblogs.com

redhat

Redhat

linuxmint

[root@localhost test]#

说明:

实例10:输出以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]#

说明:

实例12:显示包含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]#

说明:

实例13:显示当前目录下面以.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]#

说明:

(转)不看绝对后悔的Linux三剑客之grep实战精讲的更多相关文章

  1. (转)不看绝对后悔的Linux三剑客之sed实战精讲

    不看绝对后悔的Linux三剑客之sed实战精讲 原文:http://blog.51cto.com/hujiangtao/1923718 二.Linux三剑客之sed命令精讲 1,前言 我们都知道,在L ...

  2. (转)不看绝对后悔的Linux三剑客之awk实战精讲

    原文:http://blog.51cto.com/hujiangtao/1923930 一.Linux三剑客之awk命令精讲 第1章 awk基础入门 1.1 awk简介 awk不仅仅时linux系统中 ...

  3. Linux实战教学笔记12:linux三剑客之sed命令精讲

    第十二节 linux三剑客之sed命令精讲 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件 ...

  4. Linux 三剑客之 awk 实战详解教程

    我们知道 Linux 三剑客,它们分别是:grep.sed.awk.在前边已经讲过 grep 和 sed,没看过的同学可以直接点击阅读,今天要分享的是更为强大的 awk. sed 可以实现非交互式的字 ...

  5. Linux 三剑客之grep

    目录 Linux 三剑客之grep 搭配命令-find 三剑客之grep: 正则表达式: Linux 三剑客之grep 搭配命令-find find命令是根据文件的名称或者属性查找文件,并不会显示文件 ...

  6. linux三剑客之grep

    linux基础三剑客之grep 1.grep命令 基本介绍 grep命令是文本本过滤工具,是基于一个模式匹配文件的每一行,grep分类:egrep个fgrep. grep英文名:Global  sea ...

  7. Linux三剑客之grep常用参数详细总结

    三剑客grep总结 grep  : Linux三剑客老三      过滤需要的内容 参数: grep一般常用参数: -a :在二进制文件中,以文本文件的方式搜索数据 -c :计算找到 ’ 搜索字符串 ...

  8. Linux三剑客老三---grep

    1.Linux三剑客老三 过滤需要的内容,例子:grep -v oldboy hello.txt grep一般常用参数: -a:在二进制文件中,以文本文件的方式搜索数据. -c:计算找到"搜 ...

  9. Linux 三剑客之 grep 使用详解

    Linux 最重要的三个命令在业界被称为三剑客,它们是:awk.sed.grep.sed 已经在上篇中讲过,本文要讲的是 grep 命令. 我们在使用 Linux 系统中,grep 命令的使用尤为频繁 ...

随机推荐

  1. POJ1703(2集合并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39402   Accepted: ...

  2. 加固mysql服务器

    实验环境:CentOS7 [root@~ localhost]#yum -y install mariadb-server [root@~ localhost]#mysql_secure_instal ...

  3. 左连接,右连接,内连接,Union

    数据库的三种常用连接解析: 官方解释: 1.left [outer] join(左外联接) 返回 包括左表中的所有记录和右表中联结字段相等的记录 2.right [outer] join(右外联接) ...

  4. static和final的区别

    1.static是静态修饰关键字,可以修饰变量和程序块以及类方法: 当你定义一个static的变量的时候jvm会将将其分配在内存堆上,所有程序对它的引用都会指向这一个地址而不会重新分配内存: 修饰一个 ...

  5. 关于使用struts2跳转后css和js失效的解决方式

    根据观察,主要是由于通过action跳转后的url会根据命名空间,自动跳转到命名空间子目录,使得当前引用的css和js查找不到,从而失效,根据这个原因,可使用四种办法解决: 1.使用struts2.x ...

  6. MySql获取记录的名次

    在oracle中有rownum之类的东西表示记录的名次,那么在MySql中怎么获取名次呢? as rank ) B 获取的rank就是名次了 user_id rank 134762    122139 ...

  7. Learning Python 006 list(列表) 和 tuple(元组)

    Python list(列表) 和 tuple(元组) list 列表 Python内置的一种数据结构.list:一种有序的集合,可以随时添加和删除其中的元素. list的用法 定义list > ...

  8. Umbraco遇到的问题解决

    在本地VS2015运行公司的Corporate website时,有几个页面出现错误如下: 但事实是那个,这几个View都是存在的.弄了半天也没有能够解决.后来看到这个blog: https://ou ...

  9. 2.HTTP头注入

    重新认识被人遗忘的HTTP头注入 前言 注入类漏洞经久不衰,多年保持在owasp Top 10的首位.今天就聊聊那些被人遗忘的http头注入.用简单的实际代码进行演示,让每个人更深刻的去认识该漏洞. ...

  10. 复制或合并map

    1.map之间实现复制或合并 { // Map 能像数组一样被复制: let original = new Map([ [1, 'tom'] ]); let clone = new Map(origi ...