1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
[root@centos7 ~]#grep "^\(s\|S\).*" /proc/meminfo
[root@centos7 ~]#grep "^[sS].*" /proc/meminfo
[root@centos7 ~]#grep -i "^s.*" /proc/meminfo
[root@centos7 ~]#sed -n '/^[s|S]/p' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 11180 kB
Slab: 392420 kB
SReclaimable: 292556 kB
SUnreclaim: 99864 kB
2、显示/etc/passwd文件中不以/bin/bash结尾的行
[root@centos7 ~]#grep -v ".*/bin/bash$" /etc/passwd
[root@centos7 ~]#sed -n '/\/bin\/bash$/!p' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
3、显示用户rpc默认的shell程序
[root@centos7 ~]#getent passwd rpc |grep -o "[[:alnum:]/]\+$"
[root@centos7 ~]#cat /etc/passwd | grep "^\(\<rpc\>\)" |grep -o "[[:alnum:]/]\+$"
/sbin/nologin
[root@centos7 ~]#sed -n '/\brpc\b/p' /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@centos7 ~]#sed -n '/\brpc\b/p' /etc/passwd | sed -r 's@.*:(.*)$@\1@'
/sbin/nologin
4、找出/etc/passwd中的两位或三位数
[root@centos7 ~]#grep -o ":[0-9]\{2,3\}:" /etc/passwd |grep -o "[0-9]\{2,3\}"
[root@centos7 data]#grep -Ewo "[0-9]{2,3}" /etc/passwd
[root@centos7 data]#grep -Eoc "\b[0-9]{2,3}\b" /etc/passwd
5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
[root@centos7 ~]#egrep "^[[:space:]]+[^[:space:]]+" /etc/grub2.cfg
[root@centos7 ~]#sed -n '/^[[:space:]]\+[^[:space:]]\+/p' /etc/grub2.cfg
6、找出“netstat -tan” 命令结果中以LISTEN后跟任意多个空白字符结尾的行
[root@centos7 data]#netstat -tan |grep "LISTEN[[:blank:]]*"
[root@centos7 ~]#netstat -tan | sed -n '/LISTEN[[:space:]]*$/p'
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:50897 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::50928 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
7、显示CentOS7上所有系统用户的用户名和UID
[root@centos7 data]#cat /etc/passwd | cut -d: -f1,3 |tr -s ":" " "
8、添加用户bash、 testbash、 basher、 sh、 nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行
[root@centos7 root2018-07-28]#grep "^\(\<.*\>\):.*\1$" /etc/passwd
[root@centos7 ~]#sed -n '/^\b\(.*\)\b:.*\1$/p' /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
bash:x:2004:2006::/home/bash:/bin/bash
9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
[root@centos7 data]#df |grep sd |grep -o "[[:digit:]]\{1,3\}%" |grep -o "[0-9]\{1,3\}" | sort -nr
1、显示三个用户root、 mage、 wang的UID和默认shell
[root@centos7 data]#grep -Ew "^(root|wang|mage)" /etc/passwd |cut -d: -f1,7
2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
[root@centos7 ~]#egrep "^\b.*\b[[:space:]]*\(\)" /etc/rc.d/init.d/functions
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos7 data]#echo "/etc/rc.d/functions" |egrep -o "[^/.*]+$"
echo "/etc/rc.d/functions" |egrep -o "\b[^/]*+/?$"
[root@centos7 ~]#echo "/etc/rc.d/functions/" |egrep -o "[^/]+(|/)$"|egrep -o "[^/]+"
4、使用egrep取出上面路径的目录名
[root@centos7 data]#echo "/etc/rc.d/functions" |egrep -o "^/.*/" |egrep -o "^/.*[^/]"
[root@centos7 data]#echo "/etc/rc.d/functions" |egrep -o ".*\<"
5、统计last命令中以root登录的每个主机IP地址登录次数
[root@centos7 data]#last |egrep "^\broot\b" |egrep -o "[[:digit:].]{7,15}" |sort | uniq -c
lastb 失败信息
6、利用扩展正则表达式分别表示0-9、 10-99、 100-199、 200-249、 250-255
[root@centos7 data]#cat num10.txt |egrep "^[0-9]$"
7、显示ifconfig命令结果中所有IPv4地址
8、将此字符串: welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
[root@centos7 data]#echo "welcome to magedu linux" |egrep -o "." |sort | uniq -c |sort -rn