Linux正则表达式练习练习⼀
1、⽣成30位的随机⼝令
[root@centos7 ~]#cat /dev/urandom | tr -dc "[:alnum:]" | head -c30
RJL5qcA5PsQHnYE4kXui0oNkm1FNh1
2、判断主机版本号
[root@centos7 ~]#grep -o "[0-9]\+" /etc/centos-release | head -n1
练习⼆
1、出ifconfig “⽹卡名” 命令结果中本机的IPv4地址linux中netstat命令
ifconfig |egrep -o  "\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
2、查出分区空间使⽤率的最⼤百分⽐值
[root@centos7 ~]#df | grep "/dev/sd" | grep -o "[0-9]*%" | grep -o "[0-9]\+" | sort -n | tail -1
3、查出⽤户UID最⼤值的⽤户名、UID及shell类型
[root@centos7 app]#cat /etc/passwd | sort -nr -t: -k3 | head -n1 | cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin
4、查出/tmp的权限,以数字⽅式显⽰
⽅法⼀:
[root@centos7 app]#stat -c %a /tmp
1777
⽅法⼆:
[root@centos7 app]#stat /tmp | grep Uid | cut -d\( -f2 | cut -d/ -f1
1777
⽅法三
stat /tmp | grep Uid | cut -d\( -f2 | head  -c4
5、统计当前连接本机的每个远程主机IP的连接数,并按从⼤到⼩排序
显⽰⽂件/etc/init.d/functions所有⽅法
⽅法⼀
grep ".*{$" /etc/init.d/functions | tr -d {
⽅法⼆
[root@centos7 ~]#grep  -o "^.*()" /etc/init.d/functions
规范⽅法三
grep "^[[:alnum:]_]\+[[:space:]]*()" /etc/init.d/functions
练习三
1、显⽰/proc/meminfo⽂件中以⼤⼩写s开头的⾏(要求:使⽤两种⽅法,不要理解以s开头的单词)
grep -i "^s.*" /proc/meminfo    ⽅法⼀
grep  "^[Ss].*" /proc/meminfo  ⽅法⼆
2、显⽰/etc/passwd⽂件中不以/bin/bash结尾的⾏
grep -v "/bin/bash$" /etc/passwd
3、显⽰⽤户rpc默认的shell程序
grep "^rpc\>" /etc/passwd | cut -d: -f7
4、出/etc/passwd中的两位或三位数
grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
5、显⽰CentOS7的/etc/grub2.cfg⽂件中,⾄少以⼀个空⽩字符开头的且后⾯存⾮空⽩字符的⾏
grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
6、出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空⽩字符结尾的⾏
netstat -tan| grep  "LISTEN[[:space:]]*$"
7、显⽰CentOS7上所有系统⽤户的⽤户名和UID
cut -d: -f1,3 /etc/passwd | grep "\<[[:digit:]]\{,3\}$"
"\<[[:digit:]]\{,3\}\>"(123⽤户名能匹配)  注意与上正则区别
8、添加⽤户bash、testbash、basher、sh、nologin(其shell
为/sbin/nologin),出/etc/passwd⽤户名同shell名的⾏
grep "^\(.*\):.*\<\1$" /etc/passwd
9、利⽤df和grep,取出磁盘各分区利⽤率,并从⼤到⼩排序
df | grep "^/dev/sd" | grep -o "[0-9]\{1,3\}%" | grep -o "[0-9]\{1,3\}" | sort -rn
练习四
1、显⽰三个⽤户root、mage、wang的UID和默认shell
[root@centos7 ~]#grep "^\(root\)\|^\(xiaojun\)\|^\(zilong\)" /etc/passwd | cut -d: -f3,7
0:/bin/bash
1001:/bin/bash
1011:/bin/bash
2、出/etc/rc.d/init.d/functions⽂件中⾏⾸为某单词(包括下划线)后⾯跟⼀个⼩括号的⾏
[root@centos7 ~]#grep -o "^[[:alpha:]]\+\>(" /etc/rc.d/init.d/functions
checkpid(
daemon(
killproc(
…..
3、使⽤egrep取出/etc/rc.d/init.d/functions中其基名
echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+$"
扩展:取出/etc/rc.d/init.d/基名
[root@centos7 ~]#echo "/etc/rc.d/init.d/" | egrep -o "[^/]+/?$"
4、使⽤egrep取出/etc/rc.d/init.d/functions路径的⽬录名
[root@centos7 ~]#echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/" | egrep  -o  "^/.*[^/]"
5、统计last命令中以root登录的每个主机IP地址登录次数
last | grep "root" | tr -s " " ":" | cut -d : -f3 | egrep "([0-9]+.){3}[0-9]+" | uniq -c
6、利⽤扩展正则表达式分别表⽰
0-9:[0-9]
10-99  : [1-9][0-9]
100-199: 1[0-9][0-9]
200-249: 2[0-4][0-9]
250-255: 25[0-5]
7、显⽰ifconfig命令结果中所有IPv4地址
[root@centos7 ~]#ifconfig ens33 | 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])\>" 192.168.10.150
255.255.255.0
192.168.10.255