Linux grepの基本的な使い方と正規表現

11053 ワード

本文はただよく使うgrepに対して正規表現の基本的な使い方に合わせて簡単なまとめを行って、もし正規表現をよく勉強したいならば、鳥の兄Linuxの私房料理にアクセスして、台湾の同胞のウェブサイトは繁体字中国語で、少し辛抱強い必要があります.
1、grepコマンド
機能:ファイルの行ごとに文字列を検索するように入力します.
基本的な使い方:
grep [-acinv] [--color=auto] [-A n] [-B n] '     '    
    :
-a:             
-c:      
-i:       
-n:       
-AAfter   ,        n    
-B:before   ,        n    
-v:       -AAfter   ,        n -B:before   ,        n 
--color:              
 –color          ,                。      .bashrc  .bash_profile     :
alias grep=grep --color=auto

grep検索のたびに、マッチング効果が自動的にハイライトされます.'検索文字列’は正規表現です.shellのメタ文字が正規表現に与える影響を避けるために、単一引用符(’’)で囲まれ、二重引用符で囲まれたり("")囲まれたりしないでください.
2、grepと正規表現
正規表現は、基本正規表現と拡張正規表現に分けられます.それぞれ簡単にまとめてみます.
メタデータ
意味と例^word
ワードで始まる行を検索します.たとえば、#で始まるスクリプトコメント行grep –n ‘^#’ regular.txtを検索します.word$
ワードで終わる行を探す.
任意の文字に一致します.たとえば、grep –n ‘e.e’ regular.txtマッチングeとeの間には任意の文字があり、eee、eae、eveとマッチングできますが、eeとは一致しません.\
エスケープ文字.たとえば、「検索」、「」は特殊な文字で、正規表現には特殊な意味があります.まず転義しなければならない.grep –n ‘\,” regular.txt *
前の文字は0~複数回繰り返します.例えば、マッチングgle、gogle、google、googleなどgrep –n ‘go*gle’ regular.txt[list]
一連の文字の1つに一致します.例えばgl,gfをマッチングする.grep –n ‘g[lf]’ regular.txt [n1-n2]
1文字範囲の1文字を一致させます.たとえば、一致する数値文字grep –n ‘[0-9]’ regular.txt[^list]
マッチング文字セット以外の文字例えばgrep –n ‘[^o]‘ regular.txt非o文字マッチング\
単語はの先頭です.例えば、gで始まる単語grep –n ‘\に一致するword\>
前の文字はn 1,n 2回繰り返します.例えば、google,googleに一致します.grep –n ‘go\{2,3\}gle’ regular.txt \
一致する単語の末尾は、例えば、tionで終わる単語grep –n ‘tion\>’ regular.txtに一致する.word\{n1\}
前の文字はn 1を繰り返します.例えば、googleに一致します.grep –n ‘go\{2\}gle’ regular.txt word\{n1,\}
前の文字は少なくともn 1を繰り返します.例えば、google、googleに一致します.grep –n ‘go\{2\}gle’ regular.txt word\{n1,n2\}
前の文字はn 1,n 2回繰り返します.例えば、google,googleに一致します.grep –n ‘go\{2,3\}gle’ regular.txt
正規表現の拡張
?     #  0  1            。
        ,  gd,god   grep –nE ‘go?d’ regular.txt

+    #  1               ,      1   。 
       :  god,good,goood     。
     grep –nE go+d’ regular.txt

()   #           expr ,           ,
               。   :  good  glad
     grep –nE ‘g(oo|la)’ regular.txt

|    #  “ ”,         , (or)         。grep –nE ‘god|good’ regular.txt   god  good。

よく使われる集合表示方法は次のとおりです.
[[:digit:]] [0-9]

    :[[:lower:]] [a-z]

    :[[:upper:]] [A-Z]

     :[[:alpha:]] [a-zA-Z]

     :[[:alnum:]] [0-9a-zA-Z]

    :[[:space:]][[:punct:]]

3、一致するインスタンスについて
grep -c "48" test.txt #     “48”       
grep -i "May" test.txt #        “May”    )
grep -n "48" test.txt #    ;      “48”     ,    nl test.txt |grep 48)
grep -v "48" test.txt #        “48”    )
grep "471" test.txt #      “471”    )
grep "48;" test.txt #       “48”  ,    “48”    tab     
grep "48[34]" test.txt #       “48”  ,      “3”  “4”     )
grep "^[^48]" test.txt #          “48”  )
grep "[Mm]ay" test.txt #       :          “M” “m”  ,   “ay”    )
grep "K…D" test.txt #          “K”,  、 、      ,      “D”    )
grep "[A-Z][9]D" test.txt #             “A-D”,      “9”,       “D”     
grep "[35]..1998" test.txt #        3 5,         , 1998      
grep "4/{2,/}" test.txt #        :      “4”            
grep "9/{3,/}" test.txt #        :      “9”            
grep "9/{2,3/}" test.txt #        :      “9”             ,    2  3    
grep -n "^$" test.txt #         
ls -l |grep "^d" #               :ls -d *
ls -l |grep "^d[d]" #                  
ls -l |grpe "^d…..x..x" #                       

4、grep練習問題
(1)./proc/meminfoファイルの大文字または小文字で始まる行を表示します.
# grep -i '^[Ss]' /proc/meminfo

(2)./etc/passwdファイルのデフォルトshellが非/sbin/nologinのユーザーであることを表示します.
# grep -v '/sbin/nologin$' /etc/passwd | cut -d: -f1

(3)./etc/passwdファイルのデフォルトshellが/bin/bashのユーザーを表示
さらに、上記の結果のうちID番号が最も大きいユーザのみを表示する
# grep '/bin/bash$' /etc/passwd | cut -d: -f1 | sort -n -r | head -1

(4)./etc/passwdファイルの1桁または2桁を見つけます.
# grep '\<[[:digit:]]\{1,2\}\>' /etc/passwd

(5).表示/boot/grub/grub.confの少なくとも1つの空白文字の先頭の行
# grep '^[[:space:]]\+.*' /boot/grub/grub.conf

(6).表示/etc/rc.d/rc.Sysinitファイルには、#の先頭に少なくとも1つの空白文字が続き、その後、少なくとも1つの空白文字以外の行があります.
# grep '^#[[:space:]]\+[^[:space:]]\+' /etc/rc.d/rc.sysinit

(7).netstat-tanコマンド実行結果に「LISTEN」が含まれている行を特定します.
# netstat -tan | grep 'LISTEN[[:space:]]*$

(8).ユーザーbash、testbash、basher、nologin(SHELLは/sbin/nologin)を追加し、現在のシステム上のユーザー名とデフォルトのSHELLと同じユーザーを見つけます.
# grep '\(\<[[:alnum:]]\+\>\).*\1$' /etc/passwd

(9).拡張問題:次のような内容のテキストファイルを新規作成します.
He like his lover.
He love his lover.
He like his liker.
He love his liker.
その中の最後の単語を探し出すのは、前の単語にrを加えて構成された行である.
# grep '\(\<[[:alpha:]]\+\>\).*\1r' grep.txt

(10).現在のシステム上のroot、centos、またはuser 1ユーザーのデフォルトshellおよびユーザー名を表示します.
# grep -E '^(root|centos|user1\>)' /etc/passwd

(11)./etc/rcを見つけます.d/init.d/functionsファイルの単語の後ろに括弧'()の行が付いています.
# grep -o '\<[[:alpha:]]\+\>()' /etc/rc.d/init.d/functions

(12).echoを使用してパスを出力し、egrepを使用してベース名を取得します.
# echo /etc/rc.d/ | grep -o '[^/]\+/\?$' | grep -o '[^/]\+'