shell编程(四)| grep正则表达式
grep
一,grep概述
Global search regular expression and print out the line全面搜索研究正则表达式并显示出来
grep 命令是一种强大的文本搜索工具 , 根据用户指定的“模式”对目标文本进行匹配检查 , 打印匹配到的行由正则表达式或者字符及基本文本字符所编写的过滤条件确定
二,grep的基本用法和格式
grep root passwd 显示包含root的行
grep ^root passwd 显示以root开头的行
grep root$ passwd 显示以root结尾的行
grep -i root passwd 忽略大小写
grep -v root passwd 显示不包含匹配文本的所有行
grep -E "^root|ROOT$" passwd -E表示扩展的正则表达式,以root开头或者以ROOT结尾的行
注意:正规的 grep 不支持扩展的正则表达式子 , 竖线是用于表示”或”的扩展正则表达式元字符 , 正规的 grep 无法识别,egrep 命令等同于‘grep -E
####像这样带|的就是明显的扩展的正则表达式,如果不加-E就没有输出
练习
找出root在位于中间的行
- [[email protected] mnt]# cat passwd
- root:x:0:0:root:/root:/bin/bash
- bin:x:1:1:bin:/bin:/sbin/nologin
- daemon:x:2:2:daemon:/sbin:/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
- root:haha:root
- root:test:haha
- haha:root:test
- haha:test:ROOT:test
- [[email protected] mnt]# grep -i root passwd | grep -v -i ^root | grep -v -i root$ >>>>>>两次反向过滤
- haha:root:test
- haha:test:ROOT:test
- [[email protected] mnt]# grep -i root passwd | grep -v -i -E "^root|root$" >>>>>>这一条是把上面命令做了合并
- haha:root:test
- haha:test:ROOT:test
- [[email protected] mnt]# grep -v -i -E "^root|root$" passwd | grep -i root
- haha:root:test
- haha:test:ROOT:test
三,grep中的正则表达式
grep 'r..t' test 表示匹配r开头,t结尾的,中间含有两个任意字符的行,有几个.就匹配多少个
grep -E 'r*t' test 表示显示以r开始,t结尾,中间含有0到任意个r的行
grep -E 'ro*t' test 表示显示以ro开始,t结尾,中间含有0到任意个o的行,这里*表示匹配0到任意多个*前面的字符
######注意:一定要加-E否则结果不对
grep -E 'ro?t' test 表示显示以ro开头,t结尾的,中间含有0或者1个字符的行
grep -E 'ro{1,}t' test 表示显示以ro开头, t结尾,中间含有1到多个任意字符的行
grep -E 'ro{1,3}t' test 表示显示以ro开头,t结尾,中间含有1到3个任意字符的行
grep -E 'ro{,3}' test 表示显示以ro开头,t结尾,中间含有0到3个任意字符的行
grep -E 'ro{3,}t' test 表示显示以ro开头,t结尾的,中间含有3到无穷任意字符的行
grep -E 'ro+t' test 表示显示以ro开头,t结尾的,中间含有1到无穷多个任意字符的行
这与grep -E 'ro{1,}t' test 的效果一样
grep -E '(root){1,}' test 表示匹配以root开头的,含有1到无穷多个root的行
########这里root被括起来,表示一个整体!!!
grep -E 'r.*t' test 表示以r开始t结尾中间含有0到任意个任意字符的行
#####r.*t匹配的是. r*t匹配的是r
grep -E "r..\>" test 表示显示含有r(不一定是r开头),后面只匹配两个任意字符的行,\>表示防止向后扩展,有几个..就匹配几个任意字符
grep -E "\<..t" test 表示显示前面两个是任意字符,后面以t结尾的行,\<表示防止向前扩张,有几个.前面就匹配几个任意字符
练习
1,显示ip地址
- [[email protected] mnt]# ifconfig eth0 | grep -E "inet\>" | cut -d " " -f 10 >>>>> \>是直接显示inet,不扩展
- 172.25.254.100
- [[email protected] mnt]# ifconfig eth0 | grep -E "inet " | cut -d " " -f 10 >>>>> 这里在inet后面添加了空格,相当于直接搜索inet空格 ,也可以时先精确查找
- 172.25.254.100
2,显示可以登陆系统的用户
- [[email protected] mnt]# cat /etc/shells >>>>>>查看系统用户可以使用的shell
- /bin/sh
- /bin/bash
- /sbin/nologin
- /usr/bin/sh
- /usr/bin/bash
- /usr/sbin/nologin
- /bin/tcsh
- /bin/csh
- [[email protected] mnt]# grep -v nologin /etc/shells >>>>>显示用户可以登陆系统的shell
- /bin/sh
- /bin/bash
- /usr/bin/sh
- /usr/bin/bash
- /bin/tcsh
- /bin/csh
- [[email protected] mnt]# echo `grep -v nologin /etc/shells` >>>>>echo可以把多行显示为一行,这里显示为一行的目的是为了后面替换为|进行grep的与操作
- /bin/sh /bin/bash /usr/bin/sh /usr/bin/bash /bin/tcsh /bin/csh
- [[email protected] mnt]# echo `grep -v nologin /etc/shells` | sed 's/ /|/g' >>>>全局替换 把空格换为| 方便grep的与操作
- /bin/sh|/bin/bash|/usr/bin/sh|/usr/bin/bash|/bin/tcsh|/bin/csh
- [[email protected] mnt]# vim user_check.sh
- [[email protected] mnt]# cat user_check.sh
- #################################
- # Author: Minz #
- # Version: #
- # Mail: [email protected] #
- # Date: 2018-06-17 #
- # Description #
- # #
- #################################
- #!/bin/bash
- SHELL=$(echo `grep -v nologin /etc/shells` | sed 's/ /|/g')
- grep -E "$SHELL" /etc/passwd | cut -d : -f 1 管道符前面找出可以登陆用户的行,后面截取用户列
- 测试
- [[email protected] mnt]# sh user_check.sh
- root
- student
- hh
- hehe
- nihao
- [[email protected] mnt]# useradd -s /bin/tcsh user1 tcsh也可以登陆shell
- [[email protected] mnt]# sh user_check.sh
- root
- student
- hh
- hehe
- nihao
- user1
- [[email protected] mnt]# useradd -s /bin/nologin user2
- [[email protected] mnt]# sh user_check.sh
- root
- student
- hh
- hehe
- nihao
- user1