grep详解

grep 语法

grep [选项] '关键字' 文件名
grep  -n    'abc'  test

grep常见语法

OPTIONS:
    -i: 不区分大小写
    -v: 查找不包含指定内容的行,反向选择
    -w: 按单词搜索
    -o: 打印匹配关键字
    -c: 统计匹配到的行数
    -n: 显示行号
    -r: 逐层遍历目录查找
    -A: 显示匹配行及后面多少行	
    -B: 显示匹配行及前面多少行
    -C: 显示匹配行前后多少行
    -l:只列出匹配的文件名
    -L:列出不匹配的文件名
    -e: 使用正则匹配  
    -E:使用扩展正则匹配   # 也可以使用egrep
    ^key:以关键字开头
    key$:以关键字结尾
    ^$:匹配空行
    --color=auto :可以将找到的关键词部分加上颜色的显示

练习模板:

[root@bash ~]# cat /etc/passwd >> passwd
[root@bash ~]# head passwd   # head 查看前10行文本
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@bash ~]# yum -y install httpd
[root@bash ~]# cp -r /etc/httpd/conf/http.conf /root/
[root@bash ~]# head http.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
[root@bash ~]# cat test.txt 
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the li
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words

#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

练习案例

# grep -i 'root' passwd						忽略大小写匹配包含root的行
# grep -w 'ftp' passwd 						精确匹配ftp单词
# grep -w 'hello' passwd 						精确匹配hello单词;自己添加包含hello的行到文件
# grep -wo 'ftp' passwd 						打印匹配到的关键字ftp
# grep -n 'root' passwd 						打印匹配到root关键字的行好
# grep -ni 'root' passwd 						忽略大小写匹配统计包含关键字root的行
# grep -nic 'root' passwd						忽略大小写匹配统计包含关键字root的行数
# grep -i '^root' passwd 						忽略大小写匹配以root开头的行
# grep 'bash$' passwd 							匹配以bash结尾的行
# grep -n '^$' passwd 							匹配空行并打印行号
# grep '^#' /etc/vsftpd/vsftpd.conf		匹配以#号开头的行
# grep -v '^#' /etc/vsftpd/vsftpd.conf	匹配不以#号开头的行
# grep -A 5 mail passwd 				 	匹配包含mail关键字及其后5行
# grep -B 5 mail passwd 				 	匹配包含mail关键字及其前5行
# grep -C 5 mail passwd 					匹配包含mail关键字及其前后5行
利用中括号[]来查找集合字符
[] ---里面无论有几个字符,都仅代表一个字符,为‘或’关系
[^]   --- 括号里面的‘^’是取反的意思

# 查找包含shirt 或short的行
[root@bash ~]# grep -n 'sh[io]rt' test.txt 
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.

# 查找重复单个字符‘oo’的行
[root@bash ~]# grep -n 'oo' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

# 查找‘oo’前不是‘w’的行
11,12行‘oo’前面不是‘w’,而是‘o’或多个‘o’
[root@bash ~]# grep -n '[^w]oo' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

# 查找‘oo’前不是小写字母的行
[root@bash ~]# grep -n '[^a-z]oo' test.txt 
3:The home of Football on BBC Sport online.

# 查找‘oo’前不是大写字母的行
[root@bash ~]# grep -n '[^A-Z]oo' test.txt 
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

# 查找包含数字的行
[root@bash ~]# grep -n '[0-9]' test.txt 
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429


查找行首^与行尾字符$

# 小数点‘.’在正则表达式中为元字符,需要使用转义字符‘\’将其转化为普通字符
# 查找以小数点‘.’结尾的行。
[root@bash ~]# grep -n '\.$' test.txt 

# 查找空行
[root@bash ~]# grep -n '^$' test.txt 


查找任意一个字符.与重复字符*

# 查找以‘w’开头,‘d’结尾共4个字符的行
[root@bash ~]# grep -n 'w..d' test.txt 
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words

*---表示重复零个或多个前面的单字符
例:‘oo*’---第1个o必须存在,第2个o可以是0个或多个,所以o,oo,ooo等都符合规则

# 查询至少包含两个o以上的字符串
[root@bash ~]# grep -n 'ooo*' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

# 查找以‘w’开头,中间至少包含一个‘o'的,‘d’结尾的行
[root@bash ~]# grep -n 'woo*d' test.txt 
8:a wood cross!
11:#woood #
12:#woooooood #

# 查找以‘w’开头,‘d’结尾 中间字符可有可无的行
[root@bash ~]# grep -n 'w.*d' test.txt 
1:he was short and fat.
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
11:#woood #
12:#woooooood #

# 查询任意数字的行
[root@bash ~]# grep -n '[0-9][0-9]*' test.txt 
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429

# 查找连续字符范围{}
使用'.''*'可以设置零个或无限多个重复的字符
如果要限制一个范围则使用‘{}# 查看2个o的字符
[root@bash ~]# grep -n 'o\{2\}' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

# 查看w开头,d结尾,中间为2-5个o的字符串
[root@bash ~]# grep -n 'wo\{2,5\}d' test.txt 
8:a wood cross!
11:#woood #

# 查看w开头,d结尾,中间为2以上o的字符串
[root@bash ~]# grep -n 'wo\{2,\}d' test.txt 
8:a wood cross!
11:#woood #
12:#woooooood #
查找不以 井号#和空行以及包括井号#的行
grep -vE '^#|^$|#' httpd.conf 

grep 查找
-v 反向
-E 使用正则表达式
' '   里面是参数
^# 以#号开头
^$ 以^$空行开头
 # 
httpd.conf    执行文件
查找有几个yyc用户 
awk -F: '{print $1}' passwd | grep -wc 'yyc'
                                -w: 按单词搜索
                       		    -c: 统计匹配到的行数
awk 后续讲
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐