12.Linux四剑客之grep
孙富阳, 江湖人称没人称。多年互联网运维工作经验,曾负责过孙布斯大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型博客网站运维工作。
1.grep 相关选项
1.选项
选项 | 解释 |
v | 排除 |
i | 不区分大小写 |
E | 支持多条件匹配 |
n | 显示行号 |
A2 | 过滤文件内容并打印下两行 |
o | 只显示匹配到的内容 |
w | 准确匹配 |
R | 递归查找 |
l(小写L) | 只显示文件名 |
h | 只显示内容,不显示文件名 |
m | 匹配的内容,输出几行 |
c | 统计过滤的行数 |
2.示例
文件准备
[root@test /opt]# cat file.txt
I love shell
I love SHELL
This is test file
-v
[root@test /opt]# grep -v I file.txt
This is test file
-i
[root@test /opt]# grep shell file.txt
I love shell
[root@test /opt]# grep -i shell file.txt
I love shell
I love SHELL
-E -n
[root@test /opt]# grep -nE "shell|test" file.txt
1:I love shell
3:This is test file
-A
[root@test /opt]# grep -A2 "shell" file.txt
I love shell
I love SHELL
This is test file
-o
[root@test /opt]# grep -o "shell" file.txt
shell
-w
[root@test /opt]# grep -w "shell" file.txt
I love shell
2.正则表达式介绍
1.什么是正则表达式
以特定的符号表示 一组数字或字母的一种规则。
2.为什么要使用正则表达式
在工作中,我们时刻面对着大量的日志,程序,以及命令的输出。迫切的需要过滤我们需要的一部分内容,甚至是一个字符串。比如:现在有一个上千行的文件,我们仅需要其中包含”root”的行,怎么办?此时就需要使用到正则表达式的规则来筛选想要的内容。
3.正则表达式注意事项
1.正则表达式应用非常广泛,存在于各种编程语言中。
2.正则表达式和Linux多的通配符以及特殊字符是有区别的。
3.想要学会grep、sed、awk首先就要需要对正则表达式有一定的了解。只有了解了规则,才能灵活的运用。
3.正则表达式语法
1.基础和扩展正则
正则表达式 描述
\ 转义符,将特殊字符进行转义,忽略其特殊意义
^ 匹配行首,^则是匹配字符串的开始
$ 匹配行尾,$则是匹配字符串的结尾
^$ 表示空行
. 匹配除换行符\n之外的任意单个字符
[] 匹配包含在[字符]中的任意一个字符
[^] 匹配[^]之外的任意字符
[-] 匹配[]中指定范围的任意一个字符
? 匹配之前的项1次或0次
+ 匹配之前的项1次或多次
* 匹配之前的项0次或多次.*
() 匹配表达式,创建一个用于匹配的字符串
{n} 匹配之前的项n次,n是可以为0的正整数
{n,} 之前的项至少匹配n次
{n,m} 指定之前的项至少匹配n次,最多匹配m次,n<=m
| 交替匹配|两边的任意一项ab(c|d)匹配abc或abd
2.特殊符号
[[:space:]] | 空格 |
[[:digit:]] | [0-9] |
[[:lower:]] | [a-z] |
[[:upper:]] | [A-Z] |
[[:alpha:]] | [a-Z] |
3.perl正则
3.1零宽断言
(?=要匹配的内容) | 零宽度正预测先行断言(向前看) |
(?<=要匹配的内容) | 零宽度正回顾后发断言(向后看) |
[root@sfy ~]# uptime
17:51:20 up 62 days, 18:46, 1 user, load average: 0.00, 0.01, 0.05
[root@sfy ~]# uptime |grep -Po '(?<=up).*(?=,.*user)'
62 days, 18:46
3.2 perl 符号
\d | [0-9] |
\D | [^0-9] |
\s | 空字符 空格 tab 连续的空格 连续tab [\ \t\r\n\f] |
\w | [0-9a-zA-Z_] |
4.准备如下文件,然后进行正则表达式规则验证
I am sunfuyang!
I like linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http://www.buyao007.icu
our site is http://www.buyao007.icu
my qq num is 2195802440
not 2795802440
1.过滤以m开头的行
[root@test ~]# grep "^m" test.txt
2.排除空行,并打印行号
[root@test ~]# grep -nv "^$" test.txt
3.匹配任意一个字符,不包括空行
[root@test ~]# grep "." test.txt
4.匹配所有内容
[root@test ~]# grep ".*" test.txt
5.匹配以点结尾的
[root@test ~]# grep "\.$" test.txt
6.匹配有a或b或c的行
[root@test ~]# grep "[a-c]" test.txt
7.匹配数字所在的行
[root@test ~]# egrep "[0-9]" test.txt
8.匹配所有小写字母
[root@test ~]# egrep "[a-z]" test.txt
4.grep正则表达式示例
1.提取eth0的IP地址
[root@test ~]# ifconfig eth0|grep "inet "|grep -Eo "[0-9.]{1,}"|head -1
10.0.0.100
[root@test ~]# ifconfig eth0 | grep "^.*inet " | egrep -o "[[:digit:]]{2,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}"|head -1
10.0.0.100
2.排除nginx日志文件的空行和#号开头的行
[root@test ~]# egrep -v "(^#|^$|^[ ]+#)" /etc/nginx/nginx.conf
3.匹配nginx日志中的http1.0和http1.1 http2.1 http2.0
[root@sfy ~]# egrep -o "HTTP/[123].(0|1)" /var/log/nginx/access.log
4.匹配zabbix_agentd.conf配置文件中所有已启用的配置
[root@test ~]# egrep -v "^#|^$" zabbix_agentd.conf
[root@test ~]# grep '^[a-Z]' zabbix_agentd.conf
5.匹配133、152、166、135开头的手机号码
[root@test /server/script]# cat ponum.sh
#!/bin/bash
while true
do
read -p "请输入你的手机号:" po
if [[ ! $po =~ ^[0-9]{11}$ ]];then
echo 手机号不合法,请重新输入
continue
fi
if [[ $po =~ ^(133|152|166|135)[0-9]{8}$ ]];then
echo 手机号验证成功
else
echo 手机号在系统中无法搜索到
break
fi
done
未经允许不得转载:孙某某的运维之路 » 12.Linux四剑客之grep
评论已关闭