正则表达式练习

Posted N64_一只慵懒的猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则表达式练习相关的知识,希望对你有一定的参考价值。


1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

[20:12:00 root@centos8 ~][#grep "^[Ss]" /proc/meminfo [20:16:02 root@centos8 ~][#grep "^\\(S\\|s\\)" /proc/meminfo

2、显示/etc/passwd文件中不以/bin/bash结尾的行

[20:26:21 root@centos8 ~][#grep -v "\\(\\/bin\\/bash\\)" /etc/passwd

3、显示用户rpc默认的shell程序

[20:33:40 root@centos8 ~][#grep "\\(^rpc\\)" /etc/passwd | cut -d":" -f7

4、找出/etc/passwd中的两位或三位数 \\[20:38:17 root@centos8 ~][#grep -o "[0-9]\\2,3\\" /etc/passwd

5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行 \\[08:35:03 root@centos7 ~][#grep "[[:space:]]\\?\\S" /etc/grub2.cfg

6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的

[08:47:02 root@centos7 ~][#netstat -tan | grep \\(LISTEN[[:space:]]*\\)$

7、显示CentOS7上所有UID小于1000以内的用户名和UID

[08:56:29 root@centos7 ~][#cat /etc/passwd | cut -d":" -f1,3 | grep -v "[0-9]\\4,\\"

8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行

[09:14:53 root@centos7 ~][#grep "^\\(\\<.*\\>\\).*\\1$" /etc/passwd

9、利用df和grep,取出磁盘各分区利用率,并从大到小排序

[09:28:29 root@centos7 ~][#df | grep -o "[0-9]\\1,3\\[[:punct:]]" | grep -o "[0-9]\\1,3\\" | sort -nr

以上是关于正则表达式练习的主要内容,如果未能解决你的问题,请参考以下文章

*7 正则表达式的练习

Java semver 正则表达式中的未封闭字符类

匹配字符串后跟另一个字符串

正则表达式练习

正则表达式练习

正则表达式入门与练习