linux入门之文本处理三剑客(一)
1、基本用法------grep
为了使搜索结果更方便地被我们看到通常一般先对grep进行别名的设置,即
alias grep='grep --color=auto'
- 语法定义
grep 关键字 文件
- 选项与参数
-v:显示不包含关关键字的行,取反
-i :忽略关键字的大小写
-n:显示的结果每行前增加行号
-c:仅显示找出的结果的行数
-o:仅显示匹配到的关键字,不显示同行的其他内容
-q:不输出任何结果,静默
-w : 匹配整个单词
-r : 迭代查询,即在该目录及子目录下搜索
-A # 显示关键字行及向下的n行
-B # 显示关键字行及向上的n行
-C # 显示关键字行及向上n行和向下的n行
-e 关键字1 -e 关键字2 …… 多个关键字之间是或的关系
- 应用实例
例1 找出文件中包含eth的那行
[root@localhost ~]# cat test.sh
cat cat root dog hat dhjk
dog eth fgh ws lc hat jk
cek chech change wes wen root
linuc linux cut mimi wenwnen jk
[root@localhost ~]# cat test.sh |grep -n 'eth'
2:dog eth fgh ws lc hat jk
例2 找出文件中包含dog的行,忽略大小写
[root@localhost ~]# cat test.sh |grep -i 'dog'
cat cat root dog hat dhjk
Dog eth fgh ws lc hat jk
二、 与正则表达式结合的grep
- 字符匹配
. 表示任意内容
[mage] 指定内容的范围 m或a或g或e
[^mage] 除m及a及g及e
[:alnum:] 字母和数字
[:alpha:] 代表任何英文大小写字符,亦即A-Z, a-z
[:lower:] 小写字母[:upper:] 大写字母
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)
[:cntrl:] 不可打印的控制字符(退格、删除、警铃...)
[:digit:] 十进制数字[:xdigit:]十六进制数字
[:graph:]可打印的非空白字符
[:print:] 可打印字符
[:punct:] 标点符号
- 匹配次数
* 表示前面的字符匹配任意次,可以0次,可以无限,贪婪模式
.* 表示任意内容任意长度
\? 表示前面的内容匹配0次或1次
\+ 表示前的的内容匹配1次以上
\{n\}匹配前面的字符n次
\{m,n\}匹配前面的字符至少m次,至多n次
\{,n\}匹配前面的字符至多n次
\{n,\}匹配前面的字符至少n次
- 位置锚定
^ 模式最左侧
$ 模式最右侧
^PATTERN$ 用于模式匹配整行
\<或\b 词首锚定
\>或\b 词尾锚定
\1 后向引用,1代表引用前面第一个分组
\(\) 将一个或多个字符捆在一起重复列出
- 应用实例
例3 显示/proc/meminfo文件中以大小s开头的行
[root@localhost ~]# grep -i ^s /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 2592 kB
Slab: 261068 kB
SReclaimable: 182392 kB
SUnreclaim: 78676 kB
例4 找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行
[root@localhost ~]# netstat -tan|grep 'LISTEN[[:space:]]*$'
tcp 0 0 0.0.0.0:36865 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 :::53191 :::* LISTEN
tcp 0 0 :::111 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 ::1:6010 :::* LISTEN
例5 找出/etc/passwd用户名同shell名的行
[root@localhost ~]# cat /etc/passwd |grep '^\(.*\):.*/\1$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:501:501::/home/bash:/bin/bash
nologin:x:505:505::/home/nologin:/sbin/nologin
例6 利用df和grep,取出磁盘各分区利用率,并从大到小排序
root@localhost ~]# df |grep /dev/sd |grep -o '[0-9]\{1,3\}'% |sort -n -r
20%
19%
1%
例7 使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@localhost ~]# echo /etc/rc.d/init.d/functions/ |egrep -o "[^/]+/?$"
functions/
例8 使用egrep取出上面路径的目录名
[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |egrep -o ".*/." |egrep -o ".*/"
/etc/rc.d/init.d/
例9 统计last命令中以root登录的每个主机IP地址登录次数
[root@localhost ~]# last |grep root |grep '\([0-9]\{1,3\}.\)\{3\}[0-9]\{1,3\}' -o |sort | uniq -c
1 172.18.250.118
21 172.18.250.203
1 172.18.251.174
2 172.18.251.53
例10 显示ifconfig命令结果中所有IPv4地址
[root@localhost ~]# ifconfig |egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
172.18.251.84
172.18.255.255
255.255.0.0
192.168.248.129
192.168.248.255
255.255.255.0
127.0.0.1
255.0.0.0