|
SHELL之grep grep 搜索文本
grep:文本中的内容的通配
选项 模式
grep [options] PATERN file1,file2...
grep -c "2004" myfile
grep [-acinv] "搜索字符串" filename
-a 将二进制文件以文本文件的方式搜索数据
-c计算找到'搜索字符串的'次数
-i忽略大小写的不同
-n顺便输出行号
-v反向选择,即显示没有"搜索字符串"的那一行
三种变形:
-Grep 默认只支持基本正则表达式
-Egrep 支持扩展正则表达式
-Fgrep
选项:
-c 只输出匹配行的计数
-i 不区分大小写
-o 仅显示匹配到的串
-w 匹配整词
-x 匹配整行
-r 递归的搜索
-E 支持扩展正则表达式
-h 查询多文件时不显示文件名
-H 显示文件名
-l 查询多文件时只输出包含匹配字符
-n 显示匹配行及行号
-s 不显示不存在或无匹配文本的错误信息
-v 显示不包含匹配文本的所有行(反选)
-B 1:参数上面的一行
-A 1:参数下面的一行
\{n\}:重复前一个相同字符n个
\{n,m\}:重复前一个相同字符n---m个
\{n,\}:重复前一个相同字符n个以上
eg:
[iyunv@localhost shell]# grep 'root' < /etc/passwd or cat /etc/passwd | grep 'root'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
eg:
ifconfig | grep "inet addr" | grep -v "127.0.0.1" | cut -d: -f2 | cut -d' ' -f1 //取ip地址
ifconfig | grep -A 1 "^eth" | grep -o "addr:[0-9.]\{1,\}" | cut -d: -f2 //取ip地址
ifconfig | awk '{print $2}' | grep 'addr:' | cut -d ':' -f2 | grep -v '127.0.0.1' | sed '/^$/d' //取ip地址
ifconfig | grep -o "^[^[:space:]]\{1,\}" //取所有网卡
ifconfig | grep -B 1 "172.16.1.1" | grep -o "^[^[:space:]]\{1,\}" //取ip地址对应的网卡
grep "jenny" *.txt //所有txt文件中包含有jenny的
grep "sort it" * //所有文件中包含有sort it的
grep -c "2004" myfile //输出myfile文件中匹配2004的有多少行
grep -n "2004" myfile //显示myfile文件中匹配2004的行号以及行号的内容
grep -i "jul" myfile //不区分大小写的显示myfile文件中jul
grep -v "2004:22" myfile //反选过滤掉“2004:22”
grep "2004:22:5[0-9]" myfile //显示出myfile中为2004:22:50-2004:22:59
grep "^[^210]" myfile //可以过滤210,不会显示有210字符的行
grep "H*P" myfile //以p结尾,H重复出现0次或任意次 (可表示任意字符开头的,只需是p结尾的)
grep "[5-8][6-9][0-3]" myfile //显示第一个数是[5-8]第二个数是[6-9]第三个数是[0-3]之间的行
grep "4\{2\}" myfile //匹配4出现的次数为2次
grep "4\{2,\}" myfile //匹配4出现的次数至少为2次
grep "4\{2,4\}" myfile //匹配4出现的次数为2-4次之间的
grep "^$" myfile //过滤掉空格行
grep "\?" myfile //从myfile中查找出有问号的
grep "^d" myfile //显示已d开头的行
grep "^[^d]" myfile //过滤掉以d开头的行
grep '[.!]$' myfile //搜索以句号或者叹号结尾
grep '[^.!]$' myfile //不是以句号或者叹号结尾的
grep -n '^[.!]$' vsftpd //表示只有是.或者是!的行
grep -c ^$ /etc/vsftpd/vsftpd.conf //统计空格的行
grep -c [^$] /etc/vsftpd/vsftpd.conf
grep -c ^[^$] /etc/vsftpd/vsftpd.conf
grep -c ^^$ /etc/vsftpd/vsftpd.conf
[iyunv@localhost ~]# grep -c "^#" /etc/vsftpd/vsftpd.conf
103
[iyunv@localhost ~]# grep -c "^[#]" /etc/vsftpd/vsftpd.conf
103
[iyunv@localhost ~]# grep -c "^[^#]" /etc/vsftpd/vsftpd.conf
16
[iyunv@localhost ~]# grep -c "[^#]" /etc/vsftpd/vsftpd.conf
94
[iyunv@localhost ~]# grep -c "^^#" /etc/vsftpd/vsftpd.conf
0
[iyunv@localhost ~]#
eg:
[iyunv@localhost ~]# cat -A -n li //查看li这个文件里面的内容,-A参数可以显示空格和$符号
1 li. $ //注意这个li.后面有一个空格
2 tom!$
3 .!$
4 .$
5 !$
6 ni shi shui.$
7 are you ok!$
8 li jie$
9 ?$
10 $
[iyunv@localhost ~]# grep -n '[.!]' li //搜索这个文件中有.或者有!的行打印出来并显示其行号
1:li.
2:tom!
3:.!
4:.
5:!
6:ni shi shui.
7:are you ok!
[iyunv@localhost ~]# grep -n '[.!]$' li //打印以.或者!结尾的行和行号
2:tom!
3:.!
4:.
5:!
6:ni shi shui.
7:are you ok!
[iyunv@localhost ~]# grep -n '^[.!]$' li //打印只有.或者!的行和行号 (两个当中只能取一个)
4:.
5:!
[iyunv@localhost ~]# grep -n '^.$' li //匹配只有当个字符的行和行号
4:.
5:!
9:?
[iyunv@localhost ~]# grep -n '^.!$' li //匹配以.开始以!结尾中间没有任何字符的行和行号(匹配只有.!的行和行号)
3:.!
[iyunv@localhost ~]# grep -n '[^.!]$' li //匹配非.或者非!结尾的行和行号,""""!!!注意^非是在方括号里面,在外面就是以什么开头了!!!""""
1:li. //后面有空格
8:li jie
9:?
[iyunv@localhost ~]# grep -n '[^.!]' li //grep 要过滤这一行时,这一行中除了点(.)和叹号(!)之外其他的任何数字和字母都能匹配到
1:li. li. 这一行中,虽然点匹配不到,但是li这两个字母可以匹配到,所以就显示出来了
2:tom!
6:ni shi shui.
7:are you ok!
8:li jie
9:?
[iyunv@localhost ~]# grep -n '^[.!]' li //匹配以.或者!开头的行及行号
3:.!
4:.
5:!
[iyunv@localhost ~]# grep -n '.!' li //匹配!前面有任意个单字符(绝对有一个字符)???
2:tom!
3:.!
7:are you ok!
glob:文件名称通配
三次元字符
*:任意长度的任意字符
?:任意单个字符
[]:指定范围内的任意单个字符
|
|
|