Linux/Unixにおける15の実用的なgrepコマンド例


Linuxのgrepコマンドを利用する必要があります.
これは、15の詳細な例が特定のコマンドまたは機能のために提供される進行中の15の例シリーズの一部です.以前に、Linux findコマンド、linuxコマンドラインの履歴およびmysqladminコマンドの15個の実用例について議論しました.
この記事では、Linux grepコマンドの15の実用的な例をレビューします.
まず、grepコマンドを実行するために、以下の例で使用される以下のコマンドを作成します.
$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.
1つのファイル内の指定した文字列を検索する
grepコマンドの基本的な使い方は、指定されたファイルの特定の文字列を検索することです.
Syntax:
grep "literal_string" filename

$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
2 .複数のファイルで指定した文字列をチェックする.
Syntax:
grep "string" FILE_PATTERN
これはgrepコマンドの基本的な使い方です.この例では、demopersusファイルをdemoquiet file 1にコピーしましょう.grepの出力には、次のように特定のパターンにマッチした行の前にあるファイル名も含まれます.Linuxシェルがメタ文字を見たとき、それは展開を行い、grepに入力として全てのファイルを与えます.
$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
grep - iを用いた大文字小文字を区別しない検索
Syntax:
grep -i "string" FILE
これはgrepの基本的な使い方です.これは指定された文字列/パターンの場合を検出します.したがって、以下のように「」、「」、「格」のようなすべての単語に無意味にマッチします.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
ファイルに正規表現をマッチ
Syntax:
grep "REGEX" filename
これは効果的に正規表現を使用することができれば非常に強力な機能です.次の例では、「行」から始まるすべてのパターンを検索し、「空」で終了します.d . demoilesファイルの「空の何か」を検索する.

$ grep "lines.*empty" demo_file
Two lines above this line is empty.
grepのドキュメンテーションから、正規表現にはいくつかの繰り返し演算子があります.
?前の項目はオプションで、一度にマッチします.
  • 前の項目は0回以上マッチします.
  • 前の項目は1回以上マッチします.
    { n }前の項目は、正確にn倍にマッチします.
    { n }前の項目はn回以上マッチします.
    {,m }前の項目はm回最もマッチします.
    { n,m }前の項目は少なくともn回マッチしますがm回以上マッチします.
  • grep - wを使用したサブストリングではなく、完全な単語のチェック
    場合は、単語を検索し、それを避けるためには、文字列に一致する- wオプションを使用します.ただ、通常の検索を行うすべての行が表示されます.
    以下の例は通常のgrepです.“is”を検索すると、“is”、“his”、“this”が表示されます.
    $ grep -i "is" demo_file
    THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
    this line is the 1st lower case line in this file.
    This Line Has All Its First Character Of The Word With Upper Case.
    Two lines above this line is empty.
    And this is the last line.
    
    以下の例はgrepという単語です.この出力は「この行には大文字の単語の最初の文字をすべて持っています」ということに注意してください.
    $ grep -iw "is" demo_file
    THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
    this line is the 1st lower case line in this file.
    Two lines above this line is empty.
    And this is the last line.
    
    grep - a , - b , - cを使用したマッチの前後の行の表示
    巨大なファイルでgrepを行うとき、マッチの後にいくつかの行を見るのは役に立つかもしれません.grepがマッチするラインだけでなく、マッチの前に/前/行の後を表示することができれば、あなたは便利であるかもしれません.
    この例では、以下のdemoHoundテキストファイルを作成してください.
    $ cat demo_text
    4. Vim Word Navigation
    
    You may want to do several navigation in relation to the words, such as:
    
     * e - go to the end of the current word.
     * E - go to the end of the current WORD.
     * b - go to the previous (before) word.
     * B - go to the previous (before) WORD.
     * w - go to the next word.
     * W - go to the next WORD.
    
    WORD - WORD consists of a sequence of non-blank characters, separated with white space.
    word - word consists of a sequence of letters, digits and underscores.
    
    Example to show the difference between WORD and word
    
     * 192.168.1.1 - single WORD
     * 192.168.1.1 - seven words.
    
    マッチの後の6.1のディスプレイn線
    - aは以下のように指定したN行を出力するオプションです.
    Syntax:
    grep -A <N> "string" FILENAME
    
    次の例では、マッチした行を、あとの3行に沿って表示します.
    $ grep -A 3 -i "example" demo_text
    Example to show the difference between WORD and word
    
    * 192.168.1.1 - single WORD
    * 192.168.1.1 - seven words.
    6.2 Display N lines before match
    
    - bはマッチの前に指定されたn行を出力するオプションです.
    Syntax:
    grep -B <N> "string" FILENAME
    
    マッチの後にN行を表示するオプションがあれば、- Bオプションを逆にします.
    $ grep -B 2 "single WORD" demo_text
    Example to show the difference between WORD and word
    
    * 192.168.1.1 - single WORD
    
    6.3マッチの周りのn行を表示する
    - cはマッチの前に指定されたn行を出力するオプションです.いくつかの機会では、両方の側面からの行と一致するようにする必要があります.このオプションは、マッチの側(前と後)の両方の行を示します.
    $ grep -C 2 "Example" demo_text
    word - word consists of a sequence of letters, digits and underscores.
    
    Example to show the difference between WORD and word
    
    * 192.168.1.1 - single WORD
    
    greploseオプションを使用した検索の強調表示
    grepは、与えられたパターン/文字列によってファイルから行を出力するので、どの部分が行にマッチしているかを強調したい場合は、次の方法に従ってください.
    次のエクスポートを行うときは、一致する検索の強調表示を取得します.次の例では、以下に示すようにgrepCountオプション環境変数を設定すると、これをすべて強調します.
    $ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
    
    $ grep this demo_file
    this line is the 1st lower case line in this file.
    Two lines above this line is empty.
    And this is the last line.
    
    
    grep - rを使ってすべてのファイルを再帰的に検索する
    現在のディレクトリとそのサブディレクトリの下のすべてのファイルを検索したい場合.rオプションを使用する必要があります.以下の例では、カレントディレクトリ内のすべてのファイルの“ramesh”文字列を探します.
    $ grep -r "ramesh" *
    
    grepを使用した逆マッチ
    あなたが一致する行を表示するために別のオプションを持って、試合前に行を表示し、試合後の行を表示し、マッチを強調表示する.だから、間違いなく、あなたはまた、オプション- Vを反転マッチを行う必要があります.
    指定した文字列/パターンにマッチしない行を表示する場合は、下記のように- Vオプションを使用します.この例では、goという単語にマッチしない行をすべて表示します.
    $ grep -v "go" demo_text
    4. Vim Word Navigation
    
    You may want to do several navigation in relation to the words, such as:
    
    WORD - WORD consists of a sequence of non-blank characters, separated with white space.
    word - word consists of a sequence of letters, digits and underscores.
    
    Example to show the difference between WORD and word
    
    * 192.168.1.1 - single WORD
    * 192.168.1.1 - seven words.
    
    10 .指定したパターンにマッチしない行を表示します.
    Syntax:
    grep -v -e "pattern" -e "pattern"
    
    $ cat test-file.txt
    a
    b
    c
    d
    
    $ grep -v -e "a" -e "b" -e "c" test-file.txt
    d
    
    11 . grep - cを使ったマッチ数のカウント
    指定したパターン/文字列にマッチする行を数えるときには、- cオプションを使用します.
    Syntax:
    grep -c "pattern" filename
    
    $ grep -c "go" demo_text
    6
    
    どれだけの行がパターンにマッチするかを知りたいときに
    $ grep -c this demo_file
    3
    
    あなたが望むとき、パターンにマッチしない多くの線を見つけてください
    $ grep -v -c this demo_file
    4
    
    12 . grep - lを使用して与えられたパターンにマッチするファイル名だけを表示します
    grepに与えられたパターンにマッチするファイル名だけを表示したい場合は、- l (小文字のL )オプションを使用します.
    入力としてgrepに複数のファイルを与えるとき、それはパターンにマッチするテキストを含んでいるファイルの名前を表示します.そして、あなたがあなたの全ディレクトリ構造で若干のメモを見つけようとするとき、非常に便利です.
    $ grep -l this demo_*
    demo_file
    demo_file1
    
    13 .マッチした文字列のみを表示する
    デフォルトではgrepは指定したパターン/文字列にマッチする行を表示しますが、grepにマッチした文字列のみを表示したい場合は- oオプションを使用します.
    あなたがまっすぐに文字列を与えるとき、それはそれほど役に立たないかもしれません.しかし、あなたがregexパターンを与えて、それがどんなものであるかについて見るしようとするとき、それは非常に役に立ちます
    $ grep -o "is.*line" demo_file
    
    行は1番目の小文字行です
    ライン
    is is最後の行
    14 .マッチの位置を示す
    grepがファイル内のパターンにマッチする位置を表示したい場合は、次のオプションを使用します
    Syntax:
    grep -o -b "pattern" file
    
    $ cat temp-file.txt
    12345
    12345
    
    $ grep -o -b "3" temp-file.txt
    2:3
    8:3
    
    注意:上のgrepコマンドの出力は、行の位置ではなく、ファイル全体のバイトオフセットです.
    grep - nを使って出力を表示しながら行番号を表示する
    行の一致したファイルの行番号を表示するにはこれは、各ファイルの1ベースの行番号を行います.この機能を利用する- nオプションを使用します.
    $ grep -n "go" demo_text
    5: * e - go to the end of the current word.
    6: * E - go to the end of the current WORD.
    7: * b - go to the previous (before) word.
    8: * B - go to the previous (before) WORD.
    9: * w - go to the next word.
    10: * W - go to the next WORD.