Linux正則表達式及grep練習題

Linux正則表達式及grep練習題

    

    1、找出ifconfig命令結果中本機的所有IPv4地址

        # ifconfig|grep -E -o "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"

        1.png

    2、查出分區空間使用率的最大百分比值

        # df |grep "/dev/[sh]d"|tr -s ' ' '%'|cut -d"%" -f5|sort -nr|head -1

        2.png

    3、查出用戶UID最大值的用戶名、 UID及shell類型

        # sort -t: -k3 -nr /etc/passwd|head -1|cut -d: -f1,3,7

        3.png

    

    4、查出/tmp的權限,以數字方式顯示

        # stat /tmp |grep '('|cut -d"(" -f2|cut -d"/" -f1

        4.png

    5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序

        # netstat -tn|grep "tcp"|tr -s ' '|cut -d" " -f5|cut -d: -f1|sort -t"." -k4|uniq -c|sort -nr

        5.png

    6、顯示/proc/meminfo文件中以大小s開頭的行; (要求:使用兩種方式)

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

        # grep "^[Ss]" /proc/meminfo

        6.png

    7、顯示/etc/passwd文件中不以/bin/bash結尾的行

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

        blob.png

    8、顯示用戶rpc默認的shell程序

        # grep "^rpc:" /etc/passwd|cut -d: -f7

        blob.png

    9、找出/etc/passwd中的兩位或三位數

        # grep -o "\<[1-9][0-9]\{1,2\}\>" /etc/passwd|sort -nr|uniq

        blob.png

    10、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行

        # grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg

        blob.png

    11、 找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行

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

        blob.png

    12、添加用戶bash、 testbash、 basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名

    的行

        # grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd

        blob.png

    13、 顯示三個用戶root、 mage、 wang的UID和默認shell

        # egrep "^((root)|(mage)|(wang)\>)" /etc/passwd

        blob.png

    14、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行

        # egrep "^[[:alpha:]_]+\(\)" /etc/rc.d/init.d/functions

        blob.png        

    

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

        # echo  "/etc/rc.d/init.d/functions" |grep -o "[^/]\+/\?$"

        blob.png

    16、使用egrep取出上面路徑的目錄名

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

        blob.png

    17、統計以root身份登錄的每個遠程主機IP地址的登錄次數

        # last|egrep "^root.*(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).*"|tr -s " "|cut -d" " -f3|uniq -c

        blob.png 

    18、利用擴展正則表達式分別表示0-9、 10-99、 100-199、200-249、 250-255

        0-9:        [0-9]

        10-99:      [1-9][0-9]

        100-199:    1[0-9]{2}

        200-249:    2[0-4][0-9]

        250-255:    25[0-5]

    19、取本機IP地址

        # ifconfig|grep "Bcast"|cut -d":" -f2|cut -d" " -f1

        blob.png        

    20、取各分區利用率的數值

        # df |grep "/dev/[sh]d"|tr -s ' ' '%'|cut -d% -f5|sort -nr

        blob.png 

    

    21、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示

        # cat /etc/init.d/functions |tr -c "[:alpha:] \n" " "|tr -s " " "\n"|sort|uniq -c|sort -nr

        blob.png

    

    22、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目錄名

        # echo "/etc/rc.d/init.d/functions"|sed  's@[^/]\+/\?$@@'

        blob.png

    23、正則表達式表示身份證號

        1:     [1-9]

        2-6:   [0-9]{5}

        78:    (19|20|21)

        9-10   [0-9]{2}

        11-12  ((0[1-9])|11|12)

        13-14  ((0[1-9])|([12][0-9])|(3[0-1]))

        15-17  [0-9]{3}

        18     [0-9X]

        # egrep "\<[1-9][0-9]{5}(19|20|21)[0-9]{2}((0[1-9])|(10|11|12))((0[1-9])|([12][0-9])|(3[0-1]))[0-9]{3}[0-9Xx]\>" 

        blob.png

    24、正則表達式表示手機號

        分析:

            第一位 1

            第二位 3、4、5、7、8

        egrep "\<1[34578][0-9]{9}\>"

    25、正則表達式表示郵箱

        egrep -o  "\<[[:alnum:]_\-]+\.?[[:alnum:]]+@([[:alnum:]_\-]+\.)+(com|cn|edu|org|net|gov)\.?\>"

        blob.png 

    26、正則表達式表示QQ號

        # egrep "\<[1-9][0-9]{4,11}\>"

        blob.png

原創文章,作者:M20-1倪文超,如若轉載,請注明出處:http://www.www58058.com/29752

(0)
M20-1倪文超M20-1倪文超
上一篇 2016-08-07
下一篇 2016-08-07

相關推薦

  • 馬哥教育網絡班22期+第7周課程練習

    1、創建一個10G分區,并格式為ext4文件系統; [root@localhost ~]# fdisk /dev/sda 命令(輸入 m 獲取幫助):n All primary partitions are in use 添加邏輯分區 5 起始&nb…

    Linux干貨 2016-10-09
  • 第一周 N28

    作業一

    2017-12-03
  • 計算機與操作系統

    計算機系統=Hardware+Software Linux操作系統=GNU/kernel+程序 (user space) ————– apps   shell    (人機交互接口) lib call(半層)  (kernel space) —&#…

    Linux干貨 2017-02-14
  • 復制多臺虛擬機及簡單的網絡配置

    虛擬機的復制,網絡地址的簡單配置,圖形化界面setup和system-config-network的使用

    2017-09-09
  • yum——替你排憂解難的前端包安裝工具

    yum CentOS前端工具: yum, dnf 統一資源定位符:URL YUM: Yellowdog Update Modifier,rpm的前端程序,用來解決軟件包相關依賴性,可以在多個庫之間定位軟件包,自動安裝軟件包,以及該軟件包的依賴包,up2date的替代工具 yum repository: yum repo (yum倉庫)  &nbsp…

    Linux干貨 2016-08-24
  • 程序包管理

    程序包 linux的程序包主要分為兩類;二進制可執行安裝包和源代碼程序文件包     二進制應用程序的組成部分:     二進制文件、庫文件、配置文件、幫助文件   查看二進制程序所依賴的庫文件:     ldd&nbs…

    Linux干貨 2016-05-30
欧美性久久久久