Week 4 — 02 grep

#Week 04 — 02 grep

##练习  
 
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;  
cat /proc/meminfo | grep "^[Ss]"    

cat /proc/meminfo | grep -i "^s"  

    [root@VM_0_15_centos profile.d]# cat /proc/meminfo | grep “^[sS]"
    SwapCached:            0 kB
    SwapTotal:             0 kB
    SwapFree:              0 kB
    Shmem:               404 kB
    Slab:              70300 kB
    SReclaimable:      60428 kB
    SUnreclaim:         9872 kB

7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;  
cat /etc/passwd | grep -v "/sbin/nologin$" | cut -d':' -f1

    [root@VM_0_15_centos profile.d]# cat /etc/passwd | grep -v "/sbin/nologin$" | cut -d':' -f1
    root
    sync
    shutdown
    halt
    syslog
    centos
    gentoo
    mage

8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;  
cat /etc/passwd | grep "/bin/bash$" | cut -d':' -f1
    
    [root@VM_0_15_centos profile.d]# cat /etc/passwd | grep "/bin/bash$" | cut -d':' -f1
    root
    centos
    gentoo
    mage
9、找出/etc/passwd文件中的一位数或两位数;  
cat /etc/passwd | grep -o "\<[0-9]\{1,2\}\>"  
cat /etc/passwd | grep -o "\<[0-9][0-9]\?\>"

    [root@VM_0_15_centos profile.d]# cat /etc/passwd | grep -o “\<[0-9]\{1,2\}\>"
    0
    0
    1
    1
    2
    2
    3
    4
    4
    7
    5
    0
    6
    0
    7
    0
    8
    12
    11
    0
    12
    14
    50
    99
    99
    81
    81
    32
    32
    38
    38
    89
    89
    74
    74
    72
    72

10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;  
cat /boot/grub2/grub.cfg | grep "^[[:space:]]\+"  
cat /boot/grub2/grub.cfg | grep "^[[:space:]]\{1,\}"  
*用的是Centos7所以是grub2*  
*结果太长不贴*  
11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;  
cat /etc/rc.d/rc.sysinit | grep "^#[[:space:]]\+[^[:space:]]\+"  
*在Centos7不知道对应哪个文件,没有结果*  
12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;  
netstat -tan | grep "LISTEN[[:space:]]\+"  

    [root@VM_0_15_centos ~]# netstat -tan | grep “LISTEN[[:space:]]\+"
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:1723            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::111                  :::*                    LISTEN     
    tcp6       0      0 :::3306                 :::*                    LISTEN     
13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;  

    cat /etc/passwd | egrep "^([^:]*).*/\1$"

原创文章,作者:N32_xiong,如若转载,请注明出处:http://www.178linux.com/123940

发表评论

登录后才能评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.

联系我们

400-080-6560

在线咨询

工作时间:周一至周五,9:30-18:30,节假日同时也值班

QR code