正则表达式习题练习

正则表达式习题练习

1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

[root@CentOS7 ~]# grep -i ^s  /proc/meminfo   
SwapCached:            0 kB  
SwapTotal:       3145724 kB
SwapFree:        3145724 kB  
Shmem:             10292 kB  
Slab:              71952 kB  
SReclaimable:      32880 kB  
SUnreclaim:        39072 kB  
[root@CentOS7 ~]# grep  ^ [s\|S] /proc/meminfo   
SwapCached:            0 kB  
SwapTotal:       3145724 kB  
SwapFree:        3145724 kB  
Shmem:             10288 kB  
Slab:              71688 kB  
SReclaimable:      32880 kB  
SUnreclaim:        38808 kB  

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

[root@CentOS7 ~]# grep -v /bin/bash$ /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  
halt: x :7:0:halt:/sbin:/sbin/halt  
mail: x :8:12:mail:/var/spool/mail:/sbin/nologin  
operator: x :11:0:operator:/root:/sbin/nologin  
games: x :12: 100:games:/usr/games:/sbin/nologin  
ftp: x :14:50:FTP User:/var/ftp:/sbin/nologin  
nobody: x :99:99:Nobody:/:/sbin/nologin  
systemd-network: x :192:192:systemd Network Management:/:/sbin/nologin  

3、显示用户rpc默认的shell程序

[root@CentOS7 ~]# grep "^rpc\b" /etc/passwd |cut -d: -f7  
/sbin/nologin  
[root@CentOS7 ~]#   

4、找出/etc/passwd中的两位或三位数

[root@CentOS7 ~]# grep -o "\<[0-9]\{2,3\}\>" /etc/passwd   
12  
11  
12  
100  
14  
50  
99  
99  
192  

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

[root@CentOS7 ~]# grep -o "^ [[:space:]]\+ [ ^ [:space:]]\+" /etc/grub2.cfg   
  load_env  
   set  
   set  
   save_env  
   set  
   set  
  menuentry_id_option="--id"  
  menuentry_id_option=""  
  set  
  save_env  
  set  
  save_env  
  set  
  if  

6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

[root@CentOS7 ~]# netstat -tan | grep -E '(LISTEN[[:space:]]*)$'  
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       
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN       
tcp        0      0 0.0.0.0:111             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       
tcp6       0      0 ::1:631                 :::*                    LISTEN       
tcp6       0      0 ::1:25                  :::*                    LISTEN       
tcp6       0      0 ::1:6010                :::*                    LISTEN       
tcp6       0      0 :::111                  :::*                    LISTEN       
tcp6       0      0 :::22                   :::*                    LISTEN       
[root@CentOS7 ~]#   

7、显示CentOS7上所有UID小于1000以内的用户名和UID

[root@CentOS7 ~]# cat /etc/passwd |cut -d: -f1,3 |grep "\<[0-9]\{,3\}\>"  
root:0  
bin:1  
daemon:2  
adm:3  
lp:4  
sync:5  
shutdown:6  
halt:7  
mail:8  
operator:11  
games:12  
ftp:14  

8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行

useradd -s /sbin/nologin bash  
useradd -s /sbin/nologin testbash  
useradd -s /sbin/nologin basher  
useradd -s /sbin/nologin sh  
useradd -s /sbin/nologin nologin  

[root@CentOS7 ~]# grep "^\(\<.*\>\).*\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  
[root@CentOS7 ~]#   

9、利用df和grep,取出磁盘各分区利用率,并从大到小排序

[root@CentOS7 ~]# df |grep "^/dev/sd." |tr -s " " % |sort -nrt % -k 5 |cut -d% -f5  
17  
4  
1  
[root@CentOS7 ~]#   

10、显示三个用户root、mage、wang的UID和默认shell

[root@CentOS7 ~]# cat /etc/passwd  |grep -E "^(root|mage|song)"  
root:x:0:0:root:/root:/bin/bash  
song:x:1000:1000:song:/home/song:/bin/bash  
[root@CentOS7 ~]#

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

[root@CentOS7 ~]# grep -E "^\<[ ^ [:digit:]]*\>\(\)" /etc/rc.d/init.d/functions   
checkpid() {  
__kill_pids_term_kill_checkpids() {  
__kill_pids_term_kill() {  
__pids_var_run() {  
__pids_pidof() {  
daemon() {  
killproc() {  
pidfileofproc() {  
pidofproc() {  
status() {  
echo_success() {  
echo_failure() {  

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

[root@CentOS7 ~]# echo "/etc/rc.d/init.d/function" |egrep -o "[^/]+$"  
function  
[root@CentOS7 ~]#   

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

[root@CentOS7 ~]# echo "/etc/rc.d/init.d/function" |egrep -o " ^ [/].*[/]+"  
/etc/rc.d/init.d/  
[root@CentOS7 ~]#   

14、统计last命令中以root登录的每个主机IP地址登录次数

[root@CentOS7 ~]# last |grep "^root" |tr -s " " |sort -t " " -k 3 |cut -d" " -f3 |uniq -c  
     13 10.0.0.1  
      2 172.16.17.100  
[root@CentOS7 ~]#   

15、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[0-9]
[1-9][0-9]
1[0-9][0-9]
2[0-4][0-9]
25[0-5]

16、显示ifconfig命令结果中所有IPv4地址

 [root@CentOS7 ~]# ifconfig |grep "[0-9]  \ {1,3\} \ . [0-9] \ {1,3\  } \ . [0-9] \ {1,3 \ } \ . [0-9] \ {1,3 \ }"  
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255  
        inet 172.16.1.63  netmask 255.255.0.0  broadcast 172.16.255.255  
        inet 127.0.0.1  netmask 255.0.0.0  
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255  
[root@CentOS7 ~]#   

17、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

[root@CentOS7 ~]# echo welcome to magedu linux |grep -o "[a-z]" |sort |uniq -c |sort -nr  
      3 e  
      2 u  
      2 o  
      2 m  
      2 l  
      1 x  
      1 w  
      1 t  
      1 n  
      1 i  
      1 g  
      1 d  
      1 c  
      1 a  
[root@CentOS7 ~]# 
发布了12 篇原创文章 · 获赞 0 · 访问量 415

猜你喜欢

转载自blog.csdn.net/swyer_66/article/details/103646139