加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

grep与正则表达式02-相关练习题

发布时间:2020-12-14 04:24:17 所属栏目:百科 来源:网络整理
导读:(1)、显示/proc/meminfo文件中以大小s开头的行 #grep-i'^s'/proc/meminfo (2)、显示/etc/passwd文件中不以/bin/bash结尾的行 #grep-v'/bin/bash$'/etc/passwd (3)、显示/etc/passwd文件中ID号最大的用户的用户名及其shell 方法1:#cat/etc/passwd|cut-d:-f

(1)、显示/proc/meminfo文件中以大小s开头的行

#grep-i'^s'/proc/meminfo


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

#grep-v'/bin/bash$'/etc/passwd

(3)、显示/etc/passwd文件中ID号最大的用户的用户名及其shell

方法1:
#cat/etc/passwd|cut-d:-f3|sort-n|tail-1|xargsgetentpasswd|cut-d:-f1,7
nfsnobody:/sbin/nologin
方法2:
#sort-t:-k3-n/etc/passwd|tail-1|cut-d:-f1,7
nfsnobody:/sbin/nologin

(4)、显示用户rpc默认的shell程序

方法1:#grep-w'^rpc'/etc/passwd|awk-F:'{print$7}'
方法2:#grep'&;rpc&;'/etc/passwd|cut-d:-f7

(5)、找出/etc/passwd中的两位或三位数

#grep-o'[0-9]{2,3}*'/etc/passwd

(6)、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

#egrep'^[[:space:]]+[^[:space:]]'/etc/grub2.cfg

(7)、找出"netstat-tan"命令的结果中以'LISTEN'后跟0个、1个或多个空白字符结尾的行

#netstat-tan|grep"LISTEN[[:space:]]*$"


(8)、取出远程连接到本机的IP地址,并排序。

方法1:
[root@study~]#netstat-tnp|awk-F"[:]+"'NR>2{print$6}'|uniq-c|sort-r
2172.16.251.121
1172.16.250.14
方法2:
[root@study~]#netstat-tnp|tail-n+3|awk-F"[:]+"'{print$6}'|uniq-c|sort-r
2172.16.251.121
1172.16.250.14
体会这里者tail-n+3这里表示从第3行开始打印最后的行(包括第3行)
方法3:
[root@study~]#netstat-tnp|tail-n+3|cut-d:-f2|tr-s""|cut-d""-f2|uniq-c|sort-r
2172.16.251.121
1172.16.250.14
体会这里的tr-s表示将相邻两个字段之间的分隔符压缩为1个

例如:
[root@study~]#cattest.txt
1:::::::::::2:::3
[root@study~]#tr-s":"<test.txt
1:2:3


(9)、找出/etc/passwd文件中用户名和shell名一致的行

方法1:
[root@study~]#egrep"^(.*):.*1$"/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1004:1004::/home/nologin:/sbin/nologin
方法2:
[root@study~]#egrep'(&;[[:alpha:]]*&;).*1$'/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1004:1004::/home/nologin:/sbin/nologin


(10)、显示当前系统root、mage或wang用户的UID和默认shell

#egrep"^root&;|^mage&;|^wang&;"/etc/passwd|cut-d:-f3,7


(11)、找出/etc/rc.d/init.d/functions文件中某单词(包括下划线)后面跟一个小括号的行

#egrep'[[:alpha:]0-9_]+()'/etc/rc.d/init.d/functions


(12)、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@study~]#echo"/etc/rc.d/init.d/functions"|egrep-o'[[:alpha:].]+/?$'
functions
[root@study~]#echo"/etc/rc.d/init.d/"|egrep-o'[[:alpha:].]+/?$'
init.d/
好好体会一下?的妙用

最简单的方法:basename、dirname
[root@study~]#basename/etc/rc.d/init.d/functions
functions
[root@study~]#dirname/etc/rc.d/init.d/functions
/etc/rc.d/init.d


(13)、使用egrep取出上面路径的目录名

#echo"/etc/rc.d/init.d/functions"|egrep-o"^.*/"

(14)、找出ifconfig命令结果中本机的IPv4地址

[root@station126~]#ifconfigeno16777736|head-2|tail-1|cut-d""-f10
172.16.251.126
[root@station126~]#ifconfigeno16777736|awk-F""'NR==2{print$2}'
172.16.251.126
[root@station126~]#ifconfigeno16777736|awk-F"[]+"'NR==2{print$3}'
172.16.251.12

(15)、查出/tmp权限以数字形式显示

使用stat命令

方法1:
[root@study~]#stat/tmp|head-4|tail-1|egrep-o"[0-9]{4}"
1777
方法2:
[root@study~]#stat/tmp|head-4|tail-1
Access:(1777/drwxrwxrwt)Uid:(0/root)Gid:(0/root)
[root@study~]#stat/tmp|head-4|tail-1|cut-d/-f1|cut-d"("-f2
1777

未完待续...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读