Shell编程之—grep命令详解

grep 一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来

grep [option] [mode] [file]

-c #只输出匹配行的数量
-i #搜索时忽略大小写
-h #查询多文件时不显示文件名
-l #只列出符合匹配的文件名,而不列出具体的匹配行
-n #列出所有的匹配行,并显示行号
-s #不显示不存在或无匹配文本的错误信息
-v #显示不包含匹配文本的所有行
-w #匹配整词
-x #匹配整行
-r #递归搜索,不仅搜索当前工作目录,而且搜索子目录
-q #禁止输出任何结果,以退出状态表示搜索是否成功
-b #打印匹配行距文件头部的偏移量,以自己为单位
-o #与-b选项结合使用,打印匹配的词距头部的偏移量,以字节为单位
-E #支持扩展的正则表达式
-F #不支持正则表达式,按照字符串的字面意思进行匹配

grep -c root /etc/passwd #文本中有两个root匹配,即显示行数为2
grep -i xxx file #文本中有XXX和xxx,-i之后会忽略大小写都输出出来
grep -h root /etc/passwd /etc/shadow #原本查询多文件时,匹配会显示出文件名及匹配行,加-h之后只会显示匹配的行,而不会显示匹配与哪个文件
grep -l root /etc/passwd /etc/shadow #加-l之后只会显示匹配的文件名,而不列出具体的匹配行
grep -n root /etc/passwd #-n在输出匹配结果前面会加匹配的行号
grep -s root /etc/passwd #不显示错误信息,例如: grep root /etc/passwdwdwd
grep -v root /etc/passwd #列出除了匹配行root以外的所有行
grep -w root /etc/passwd #列出文件中有单个root的单词的行
grep -x root /etc/passwd #列出文件中有单个root为整行的行
grep -r root /tmp #列出/tmp下面的root及子目录文件下的root,递归查询出所有的root字符
grep -q root /tmp/root #不输出任何信息,以退出方式,成功为0,不成功为其他值

grep –vc root /etc/passwd /etc/shadow #显示出文件pass和sha中有多少行是不包含root的行

1、匹配行首
(1)、检索”/etc/passwd”中以”root”开头的行,并打印出行号
grep -n ^root /etc/passwd
(2)、检索”/etc/services”文件中的空行,列出行号。再次检索空行有几行
grep -n ^$ /etc/services
grep -c ^$ /etc/services
(3)、搜索文件中以”-“开头,并且重复任意次,然后是D字符的匹配行
搜索文件中以”/”开头,中间”4″个任意字符,第”6″个字符仍为”/”的匹配行
grep ^-*D filename
grep ^/…./ filename
(4)、搜索”sed.edu.cn”的匹配行
grep sed\.edu\.cn
(5)、搜索”-“符号重复5次的文本行
搜索”the”精确匹配的行
grep ‘\-\{5\}’ filename #考验”和\{\}表达式的特性
grep ‘\<the\>’ filename #考验”和\<\>表达式的特性

grep #标准grep命令。支持正则表达式
egrep #扩展grep命令。支持基本和扩展正则表达式
fgrep #快速grep命令。不支持正则表达式,按照字符串的字面意思进行匹配

egrep 命令与 grep -E等价
fgrep 命令与 grep -F等价

分析下面的正则表达式表达了什么含义
(1)、kK* grep kK* test #列出文件test包含kK及后面有任意多个字符的行
(2)、k\{6,8\} grep ‘k\{6,8\}’ test #列出文件test中k单词重复6-8个的行
(3)、k\{6,\} grep ‘k\{6,\}’ test #列出文件test中k单词大于6个的行
(4)、k\{10\} grep ‘k\{6\}’ test #列出文件test中k单词为6个的行
(5)、^NEW YEAR$
(6)、^$ #空行
(7)、[0-9][0-9][a-z] grep [0-9][0-9][a-z] file #列出文件中前两个字符为数字,后一个字符为字母的匹配行
(8)、[A-H]\{1,3\},[0-9]\{5\} #grep ‘[a-h]\{1,3\}’ test | grep ‘[0-9]\{5\}’ #列出1-3个a-h单词,5个数字的字符
(9)、^\…
(10)、[^p-z]*\. #列出除了p-z以外的多个任意字符的行

2、利用通配功能列出某目录下所有以数字开头,最后3为是句点和2个任意字母的文件名
[0-9]*[a-z]\{2\}…

3、查看下面三条命令
grep -c ^$ filename #列出空行的数目
grep -c ^[^$] filename #列出非空行的数目
grep -c ^^$ filename

4、统计当前目录及子目录下的所有文件所包含空白行的行数
grep -r ^$ /root/ | wc -l

5、统计当前目录及子目录下的所有文件包含非空白行的行数
grep -r ^[^$] /root/ | wc -l

6、结合对-符号的阐述,观察是否对错
grep -n -\{5,\} file #缺引号,缺转义符\-
grep -n ‘-\{5,\}’ file #缺转义符,系统默认不知道-是选项
grep -n ‘\-\{5,\}’ file #正确

7 Responses so far.

  1. quest bars says:
    Genuinely when someone doesn’t understand afterward its up to
    other visitors that they will assist, so here it occurs.
  2. quest bars says:
    It’s going to be ending of mine day, however before finish I am reading this wonderful piece of writing to
    increase my experience.
  3. As a Newbie, I am always browsing online for articles that can help me. Thank you
    • Pokey says:
      Hello just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Safari. I’m not sure if this is a formatting issue or something to do with internet browser coimtaibplity but I figured I’d post to let you know. The design and style look great though! Hope you get the problem solved soon. Cheers
  4. Susie says:
    Hey, good to find somonee who agrees with me. GMTA.

Leave a Reply to Bayle Cancel reply