LINUX grep练习题
1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两 种方法)
代码:
cat /proc/meminfo|grep -i ^s
执行结果
2、显示/etc/passwd文件中不以/bin/bash结尾的行
代码:
cat /etc/passwd |grep -v /bin/bash$
3、显示用户rpc默认的shell程序
代码:
cat /etc/passwd |grep ^”\brpc\b”|cut -d/ -f5,6
4、找出/etc/passwd中的两位或三位数
代码:
cat /etc/passwd|grep “\<[0-9]{2,3}>”
5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白 字符开头的且后面存非空白字符的行
代码:
cat /etc/grub2.cfg |grep “^[[:space:]] [^[:space:]]”
6、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多 个空白字符结尾的行
代码:
netstat -tan |grep “LISTEN”$’ ‘*
7、显示CentOS7上所有系统用户的用户名和UID
cat /etc/passwd|cut -d: -f1,3|grep “\b[0-9]{0,3}$”
8、添加用户bash、testbash、basher、sh、nologin(其shell 为/sbin/nologin),找出/etc/passwd用户名同shell名的行
代码:cat /etc/passwd | grep “(^.)>./\1$”
9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
代码
df|grep dev |grep -o “[0-9]{1,3}%”|sort -rn
1、显示三个用户root、mage、wang的UID和默认shell
grep -E “^(root|mage|wang)>” /etc/passwd | cut -d: -f1,3,7
2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包 括下划线)后面跟一个小括号的行
[[email protected] app]# grep -E “^[[:alpha:]_]+()” /etc/rc.d/init.d/functions
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo /etc/rc.d/init.d/functions | grep -E -o “[^/]+/?$”
4、使用egrep取出上面路径的目录名
echo /etc/rc.d/init.d/functions |grep -E -o “/.*/”
“`