basic regular expression BRE
//基础正则符号 1.^以什么开头的信息 2.$以什么结尾的信息 [root@lbz01 ~]# cat >>/lbz/lbz.txt<<EOF //创建测试环境 > # lbz-sz-01 > lbz-linux > lbz-python-linux > > # lbz-sh > lbz-linux > lbz-python-linux > > # lbz-bj > lbz-linux > java > mysql > go > python > EOF [root@lbz01 ~]# grep "^lbz" /lbz/lbz.txt 1.将以lbz开头的行找出来 lbz-linux lbz-python-linux lbz-linux lbz-python-linux lbz-linux [root@lbz01 ~]# grep "linux$" /lbz/lbz.txt 2.将以lbz结尾的行找出来 lbz-linux lbz-python-linux lbz-linux lbz-python-linux lbz-linux [root@lbz01 ~]# grep -v "^$" /lbz/lbz.txt // 将全部内容显示,但不要显示空行 # lbz-sz-01 lbz-linux lbz-python-linux # lbz-sh lbz-linux lbz-python-linux # lbz-bj lbz-linux java mysql go python //基础正则符号 1. .符号 2. *星符号 3. .*点星结合 4. \转义符号 5. []括号符号 6. [^]括号和尖号组合 [root@lbz01 ~]# cat >>~/lbz_test.txt<<EOF //创建实验环境 > I am lbz teacher! > I teach linux. > > I like badminton ball ,billiard ball and chinese chess! > my blog is http://lbz.blog.51cto.com > our site is http://www.etiantian.org > my qq num is 49000448. > > not 4900000448. > my god ,i am not lez,but LBZ! > EOF [root@lbz01 ~]# grep "." ~/lbz_test.txt (-o) 1. .符号 #. 匹配任意一个字符,且只有一个字符(-o显示匹配过程) I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my qq num is 49000448. not 4900000448. my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "0*" ~/lbz_test.txt 2. *符号 #匹配前一个字符连续出现了0次或者多次 I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my qq num is 49000448. not 4900000448. my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "^m.*m$" ~/lbz_test.txt 3. .*点星结合 # .* 匹配任意所有信息 my blog is http://lbz.blog.51cto.com [root@lbz01 ~]# grep "^m.*o" ~/lbz_test.txt //贪婪匹配 my blog is http://lbz.blog.51cto.com my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "^m.*to" ~/lbz_test.txt //指定具体信息阻止贪婪匹配 my blog is http://lbz.blog.51cto.com [root@lbz01 ~]# echo -e "lbz01\nlbz02\nlbz03" 4. \转义符号 \作用: 01. 将有特殊意义符号,转义成普通信息进行识别 02. 将没有意义的信息转义为有特殊意义的信息 \n 换行符号 \t 制表符号 lbz01 lbz02 lbz03 [root@lbz01 ~]# grep ".$" lbz_test.txt //使用转义符号前 I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my qq num is 49000448. not 4900000448. my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "\.$" lbz_test.txt //使用转义符号后 I teach linux. my qq num is 49000448. not 4900000448. [root@lbz01 ~]# grep "l[be]z" lbz_test.txt 5. []括号符号 #[] 匹配多个字符信息 I am lbz teacher! my blog is http://lbz.blog.51cto.com my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "^[Im]" lbz_test.txt I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com my qq num is 49000448. my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "[abc]" lbz_test.txt 6. [^]括号和尖号组合 #[^] 排除符号 I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my god ,i am not lez,but LBZ! [root@lbz01 ~]# grep "[^abc]" lbz_test.txt I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my qq num is 49000448. not 4900000448. my god ,i am not lez,but LBZ!extended regular expression ERE
//扩展正则符号 默认 grep sed不能直接识别 grep 提升自己 -- egrep/grep -E sed 提升自己 -- sed -r 1. +号符号:+ 2. 竖线符号:| 3. 括号符号:() 4. 括号符号:{} 5. 问号符号:? 1. +号符号:+ [root@lbz01 ~]# cat lbz_test.txt //实验环境 I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my qq num is 49000448. not 4900000448. my god ,i am not lez,but LBZ! [root@lbz01 ~]# egrep "0+" lbz_test.txt 1.+号符号 #+:匹配前一个字符连续出现了1次或者多次 my qq num is 49000448. not 4900000448. [root@lbz01 ~]# egrep "0+" lbz_test.txt -o 000 00[root@lbz01 ~]# grep -Ev "0+" lbz_test.txt //排除内容 I am lbz teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my god ,i am not lez,but LBZ! 000 2. 竖线符号:| [root@lbz01 ~]# grep -E "lbz|lez" lbz_test.txt 2. 竖线符号:| #|:并且符号,用于匹配多个信息 I am lbz teacher! my blog is http://lbz.blog.51cto.com my god ,i am not lez,but LBZ! 3. 括号符号:() [root@lbz01 ~]# grep -E "l(be)z" lbz_test.txt 3. 括号符号:() #():指定信息进行整体匹配 [root@lbz01 ~]# grep -E "l(b|e)z" lbz_test.txt I am lbz teacher! my blog is http://lbz.blog.51cto.com my god ,i am not lez,but LBZ! #():进行后项引用前项使用: sed命令替换功能 [root@lbz01 ~]# echo lbz{01..10}|xargs -n 1|sed -r 's#(.*)#useradd\1#g' useraddlbz01 useraddlbz02 useraddlbz03 useraddlbz04 useraddlbz05 useraddlbz06 useraddlbz07 useraddlbz08 useraddlbz09 useraddlbz10 [root@lbz01 ~]# seq -w 10|sed -r 's#(.*)#useradd lbz\1;echo 123456|passwd --stdin lbz\1#g' //批量创建用户 lbz01..lbz10,并给每个用户设置密码信息,默认初始化密码(123456) useradd lbz01;echo 123456|passwd --stdin lbz01 useradd lbz02;echo 123456|passwd --stdin lbz02 useradd lbz03;echo 123456|passwd --stdin lbz03 useradd lbz04;echo 123456|passwd --stdin lbz04 useradd lbz05;echo 123456|passwd --stdin lbz05 useradd lbz06;echo 123456|passwd --stdin lbz06 useradd lbz07;echo 123456|passwd --stdin lbz07 useradd lbz08;echo 123456|passwd --stdin lbz08 useradd lbz09;echo 123456|passwd --stdin lbz09 useradd lbz10;echo 123456|passwd --stdin lbz10 [root@lbz01 ~]# echo 123456|sed -r 's#([1-6]+)#<\1>#g' //将输出的信息echo 123456 ==> 转换为 <123456>进行显示 <123456> [root@lbz01 ~]# echo 123456|sed -r 's#(12)(34)(56)#<\1><\2><\3>#g' #echo 123456 ==> 转换为 <12><34><56>进行显示 <12><34><56> [root@lbz01 ~]# echo 123456|sed -r 's#(12)(34)(56)#<\1><><\3>#g' <12><><56> [root@lbz01 ~]# echo 123456|sed -r 's#(12)(34)(56)#<\1><\3><\3>#g' <12><56><56> [root@lbz01 ~]# echo 123456|sed -r 's#(..)(..)(..)#<\1><\2><\3>#g' <12><34><56> 4. 括号符号:{} #可以指定字符连续匹配的次数 4.括号符号:{} [root@lbz01 ~]# grep -E "0{2,3}" lbz_test.txt #第一种情况: x{n,m} 表示前一个字符至少连续出现n次,最多出现m次 my qq num is 49000448. not 4900000448. [root@lbz01 ~]# grep -E "0{2,3}" lbz_test.txt -o 000 000 00 [root@lbz01 ~]# grep -E "0{2}" lbz_test.txt #第二种情况: x{n} 表示前一个字符正好连续出现了n次 my qq num is 49000448. not 4900000448. [root@lbz01 ~]# grep -E "0{2}" lbz_test.txt -o 00 00 00 [root@lbz01 ~]# grep -E "0{2,}" lbz_test.txt #第三种情况: x{n,} 表示前一个字符至少连续出现n次,最多出现多少次不限 my qq num is 49000448. not 4900000448. [root@lbz01 ~]# grep -E "0{2,}" lbz_test.txt -o 000 00000 [root@lbz01 ~]# grep -E "0{,2}" lbz_test.txt #第四种情况: x{,m} 表示前一个字符至少连续出现0次,最多出现m次 I am lbz teacher! I teach linux. [root@lbz01 ~]# grep -E "0{,2}" lbz_test.txt -o 00 0 00 00 0 5. 问号符号:? 定义匹配前一个字符出现0或者1次 5.问号符号:? [root@lbz01 ~]# cat >>lbz_test03.txt<<EOF //创建测试环境 > gd > god > good > goood > gooood > EOF [root@lbz01 ~]# egrep "o?" lbz_test03.txt gd god good goood gooood [root@lbz01 ~]# egrep "o?" lbz_test03.txt -o o o o o o o o o o o 补充:想让grep sed命令可以直接识别扩展正则 [root@lbz01 ~]# grep "o\+" lbz_test03.txt god good goood gooood [root@lbz01 ~]# echo 123456|sed "s#\(..\)\(..\)\(..\)#<\1><\2><\3>#g" <12><34><56> //区分: * 匹配前一个字符出现0次或者多次 + 匹配前一个字符出现1次或者多次 {} 匹配前一个字符指定出现多少次,至少0次 ? 匹配前一个字符出现0次或者1次 补充:利用ip address show eth0只把IP地址信息显示出来 [root@lbz01 ~]# ip a s eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UPgroup default qlen 1000 link/ether 00:0c:29:9b:9b:9c brd ff:ff:ff:ff:ff:ff inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::f36b:6d73:ec43:617c/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@lbz01 ~]# ip a s eth0|grep "[0-9].[0-9].[0-9].[0-9]" inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|grep "[0-9]+.[0-9]+.[0-9]+.[0-9]+" [root@lbz01 ~]# ip a s eth0|egrep "[0-9]+.[0-9]+.[0-9]+.[0-9]+" inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|egrep "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|egrep "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" -o 10.0.0.200 10.0.0.255 [root@lbz01 ~]# ip a s eth0|egrep "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" -o|head -1 10.0.0.200 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.)" inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.)" -o 10. 0. 0. 10. 0. 0. [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.){3}[0-9]+" -o 10.0.0.200 10.0.0.255 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.?){4}" -o 1500 1000 10.0.0.200 10.0.0.255 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.?){4}" 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UPgroup default qlen 1000 inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.?){4}"|head -3 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UPgroup default qlen 1000 inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.?){4}"|head -3|tail -1 inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@lbz01 ~]# ip a s eth0|egrep "([0-9]+\.?){4}" -o|head -3|tail -1 10.0.0.200问题及反思:
实验环境: [root@lbz01 ~]# cat lbz_test.txt I am $lbz teacher! I \teach \linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://lbz.blog.51cto.com our site is http://www.etiantian.org my qq num is 49000448. not 4900000448. my god ,i am not lez,but LBZ! 01. 文件中有\撬棍信息,如何进行过滤 [root@lbz01 ~]# grep '\' lbz_test.txt grep: Trailing backslash [root@lbz01 ~]# grep '\\' lbz_test.txt I \teach \linux. 02. 文件中有$符号, 是识别变量, 还是识别普通符号 [root@lbz01 ~]# grep '$lbz' lbz_test.txt I am $lbz teacher! [root@lbz01 ~]# grep '\$lbz' lbz_test.txt I am $lbz teacher!总结: