Linux四劍客之grep

 

grep

一、基礎操作

                 0)過濾查找,匹配文件裏面的某個字符、內容

                 1)查找文件裏面的某個內容

                      grep "root" /etc/passwd

                 2)查找文件裏面的某個內容並且加顏色

                      grep --color "root" /etc/passwd

                 3)查找文件裏面的某個內容並且加顏色加行號

                      grep -n --color "root" /etc/passwd

                 4)查找文件裏面的某個內容並且加顏色加行號以該內容開頭

                      grep -n --color "^root" /etc/passwd

                 5)查找文件裏面的某個內容並且加顏色加行號以該內容結尾

                       grep -n --color "bash$" /etc/passwd

                 6)查找某個文件裏面以#開頭的

                      grep "#" usr/local/conf.xxx.default

                 7)查找某個文件裏不以#開頭的

                      grep -v "#" usr/local/conf.xxx.default

                      -v取反

                      去空行

                            grep -v "#" usr/local/conf.xxx.default |grep -v "^$"

二、基礎正則表達式

                 grep -[acinv] '搜索內容串' filename

                 -a 以文本文件方式搜索

                 -c 計算找到的符合行的次數

                      grep -c "may" test.txt

                 -i 忽略大小寫

                      grep -i "may" test.txt

                 -n 順便輸出行號

                 -v 反向選擇,即顯示不包含匹配文本的所有行

                 -h 查詢多文件時不顯示文件名

                 -l 查詢多文件時只輸出包含匹配字符的文件名

                 -s 不顯示不存在或無匹配文本的錯誤信息

                 grep 命令加 -E 參數,這一擴展允許使用擴展模式匹配

                 案例:匹配ip地址

                      egrep --color "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" test.txt

                      egrep --color "([0-9]{1,3}\.){3}[0-9]{1,3}$" test.txt

                 grep正則表達式元字符集(基本集)

     

 

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