您的位置:首页 > 其它

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

2016-05-22 12:13 330 查看
(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|xargs getent passwd|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
2 172.16.251.121
1 172.16.250.14
方法2:
[root@study ~]# netstat -tnp|tail -n +3|awk -F "[ :]+" '{print $6}'|uniq -c|sort -r
2 172.16.251.121
1 172.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
2 172.16.251.121
1 172.16.250.14
体会这里的tr -s  表示将相邻两个字段之间的分隔符压缩为1个

例如:
[root@study ~]# cat  test.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 ~]# ifconfig eno16777736|head -2|tail -1|cut -d " " -f10
172.16.251.126
[root@station126 ~]# ifconfig eno16777736|awk -F" " 'NR==2{print $2}'
172.16.251.126
[root@station126 ~]# ifconfig eno16777736|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
未完待续...

本文出自 “Hello,Linux” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: