8 Linux之grep

  • 額,這裏不能抱怨由於網絡原因,幾次寫博客都被牛逼哄哄的網速給回了,dan疼啊!

    • 文本搜索工具:grep,egrep,fgrep

1(1)grep "^[[:alpha:]]*" /proc/meminfo

  (2)

grep "^[a-zA-Z]\+" /proc/meminfo

2 grep -v ".*\/sbin\/nologin.*" /etc/passwd | cut -d: -f1

     grep -v ".*\/sbin\/nologin$" /etc/passwd | cut -d: -f1

3 grep ".*\/bin\/bash.*" /etc/passwd | cut -d: -f1

     grep ".*\/bin\/bash$" /etc/passwd | cut -d: -f1

4 grep "\<[[:digit:]][[:digit:]]\>" /etc/passwd --color=auto

   

 grep "\<[[:digit:]]\{2\}\>" /etc/passwd --color=auto

5

grep "^[[:space:]]\{1,\}.*" /boot/grub/grub.conf
     grep "^[[:space:]]\+"

6 顯示當前系統上root、fedora、或user1用戶的默認shell

   

 egrep --color "^root|^fedora|^user1" /etc/passwd | cut -d: -f1,7
     egrep --color "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7


7 找出/etc/rc.d/init.d/functions 文件中某單詞後面跟一組小括號的行,如:hello()

     

egrep --color=auto "*\<[[:alnum:]]+\>\(\)" /etc/rc.d/init.d/functions |cut -d' ' -f1

     -o選項

8 echo "/etc/sysconfig" | grep 

     # basename /etc/sysconfig/

          sysconfig

echo "etc/sysconfit/net-scripts"| egrep --color=auto "*[^/]+$"

     # basename /etc/sysconfig

          sysconfig

     擴展:取出其路徑名

9 找出ifconfig命令結果中的1-255之間的數字

ifconfig | egrep "\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\>" --color=auto

10 挑戰:寫個模式,能匹配合理的IP地址

     

11 挑戰:寫個模式,能匹配出所有的郵件地址

     由於具體郵件地址規則不詳,故下面是自己寫的,感覺還不錯

egrep --color=auto "^[[:alnum:]]+(_?[[:alnum:]]+){0,}@([[:alnum:]]+\.){0,}[[:alnum:]]+$" matchmail.regexp




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章