首页 > 其他 > 详细

正则表达式练习题

时间:2020-05-04 12:01:37      阅读:77      评论:0      收藏:0      [点我收藏+]

1.显示/proc/meminfo文件中以大小s开头的行(要求两种方法)
cat /proc/meminfo | grep ‘^[sS]‘
cat /proc/meminfo | grep ‘^s\|^S‘

2.显示/etc/passwd文件中不以/bin/bash结尾的行
cat /etc/passwd | grep -v "/bin/bash"

3.显示用户rpc默认的shell程序
cat /etc/passwd |grep ‘rpc‘ | cut -d‘/‘ -f5-6

4.找出/etc/passwd中的两位或三位数
cat /etc/passwd |grep -o ‘[0-9]\{2,3\}‘

5.显示centos7的/etc/grub2.cfg文件中,至少以一个空白字符开头且后面有非空白字符的行
grep ‘^[[:space:]].\+[^[:space:]]‘ /etc/grub2.cfg

6.找出netstat -tan命令结果中以LISTEN后跟任意多个空白字符结尾的行
netstat -tan | grep ‘LISTEN[[:space:]]*$‘

7.显示centos7上所有UID小于1000以内的用户名和UID
cat /etc/passwd |cut -d: -f1,3 |grep ‘\<[0-9]\{1,3\}\>‘|sort -t: -k2 //默认正向排序
cat /etc/passwd |cut -d: -f1,3 |grep ‘\<[0-9]\{1,3\}\>‘|sort -t: -k2 -nr //也可对k2列反向排序

8.添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行
cat /etc/passwd |grep ‘\(^[[:alnum:]]\+\b\).*/\1$‘

9.利用df和grep,取出磁盘各分区利用率,并从大到小排序
[16:48:38 root@localhost data]#df |tr -s ‘ ‘ |cut -d ‘ ‘ -f5-6 |tail -n +2 |sort -k1 -nr
27% /
16% /boot
2% /swap
2% /run
1% /run/user/0
0% /sys/fs/cgroup
0% /dev/shm
0% /dev

 

正则表达式练习题

原文:https://www.cnblogs.com/HRAS/p/12825860.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!