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地址 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

猜你喜欢

转载自www.cnblogs.com/tanxiaojun/p/10473141.html