GNU/Linux >> Linux の 問題 >  >> Linux

Linux / UNIXでの15の実用的なgrepコマンドの例


写真提供:Alexôme’s

Linux の grep コマンドを理解する必要があります。

これは、進行中の 15 の例シリーズの一部であり、特定のコマンドまたは機能について 15 の詳細な例が提供されます。 Linux の find コマンド、Linux のコマンドライン履歴、mysqladmin コマンドの 15 の実際的な例について説明しました。


この記事では、初心者にも専門家にも非常に役立つ Linux grep コマンドの 15 の実例を紹介します。


最初に、次の例で使用される次の demo_file を作成して、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.指定された文字列を 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コマンドの基本的な使い方です。この例では、demo_file を demo_file1 にコピーします。 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.

3. grep -i を使用した大文字と小文字を区別しない検索

Syntax:
grep -i "string" FILE


これもgrepの基本的な使い方です。これは、指定された文字列/パターンの大文字と小文字を区別せずに検索します。そのため、以下に示すように、大文字と小文字を区別せずに、「the」、「THE」、「The」などのすべての単語に一致します。

$ 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.

4.ファイル内の正規表現に一致

Syntax:
grep "REGEX" filename


これは、正規表現を効果的に使用できれば非常に強力な機能です。次の例では、「lines」で始まり「empty」で終わるすべてのパターンを検索します。つまり、demo_file で「行[間にあるもの]空」を検索します。

$ grep "lines.*empty" demo_file
Two lines above this line is empty.

grep のドキュメントから:正規表現の後に、いくつかの繰り返し演算子のいずれかが続く場合があります:

  • ?前の項目はオプションで、最大 1 回一致します。
  • * 前の項目は 0 回以上一致します。
  • + 前の項目が 1 回以上一致します。
  • {n} 前の項目は正確に n 回一致します。
  • {n,} 前の項目が n 回以上一致しています。
  • {,m} 前の項目は最大で m 回一致します。
  • {n,m} 前の項目は少なくとも n 回一致しますが、m 回以下です。

5. grep -w を使用して部分文字列ではなく単語全体をチェックする

単語を検索して、部分文字列との一致を回避するには、-w オプションを使用します。通常の検索を行うだけで、すべての行が表示されます。

次の例は、「is」を検索する通常の grep です。オプションなしで「is」を検索すると、「is」、「his」、「this」、および部分文字列「is」を含むすべてが表示されます。

$ 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.


次の例は、単語「is」のみを検索する WORD grep です。この出力には、「This Line Has All Its First Character With The Word With Upper Case」という行が含まれていないことに注意してください。「This」には「is」がありますが、次の例では「is」という単語のみを検索しているためです。 」であり、「これ」ではありません。

$ 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.

6. grep -A、-B、および -C を使用して、試合の前/後/前後の行を表示する

巨大なファイルに対して grep を実行する場合、一致した後のいくつかの行を確認すると便利な場合があります。 grep が一致する行だけでなく、一致する行の後/前/前後の行も表示できると、便利に感じるかもしれません.


この例では、次の demo_text ファイルを作成してください。

$ 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 試合前に N 行を表示する

-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 行を出力するオプションです。場合によっては、試合を両側からのラインで表示したい場合があります。このオプションは、マッチの両側 (前後) に 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

7. GREP_OPTIONS を使用した検索の強調表示

grep は指定したパターン/文字列でファイルから行を出力するので、行に一致する部分を強調表示したい場合は、次の方法に従う必要があります。

次のエクスポートを実行すると、一致した検索が強調表示されます。次の例では、以下に示すように GREP_OPTIONS 環境変数を設定すると、これらすべてが強調表示されます。

$ 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.

8. grep -r を使用してすべてのファイルを再帰的に検索する

現在のディレクトリとそのサブディレクトリ以下のすべてのファイルを検索したい場合。 -r オプションは、使用する必要があるオプションです。次の例では、現在のディレクトリとそのすべてのサブディレクトリ内のすべてのファイルで文字列「ramesh」を検索します。

$ grep -r "ramesh" *

9. grep -v を使用して一致を反転

一致した行を表示する、一致する前に行を表示する、一致した後に行を表示する、一致を強調表示する、さまざまなオプションがありました。したがって、間違いなく -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 は指定されたパターン/文字列に一致する行を表示しますが、パターンに一致した文字列のみを表示する場合は、-o オプションを使用します。

文字列をまっすぐに与えると、あまり役に立たないかもしれません。しかし、正規表現パターンを与えて、それが何と一致するかを見ようとするとき、それは非常に役に立ちます

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line

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 コマンドの出力は、行内の位置ではなく、ファイル全体のバイト オフセットです。

15. 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.

追加の grep チュートリアル

  • 7 Linux Grep OR、Grep AND、Grep NOT 演算子の例
  • Grep コマンドの正規表現と 10 の例 – パート I
  • grep コマンドの高度な正規表現と 10 の例 – パート II
  • bzgrep を使用して *.bz2 ファイルを検索し、zgrep を使用して *.gz ファイルを検索します

すばらしい Linux 記事

以下は素晴らしい 15 の例です。 役に立つと思われる記事

  • Linux Crontab:15 の素晴らしい Cron ジョブの例
  • ママ、見つけたよ! — 15 の実用的な Linux 検索コマンドの例
  • Linux コマンドラインの歴史をマスターするための 15 の例
  • Unix LS コマンド:15 の実例

Linux
  1. Linuxでのcpコマンド:7つの実用的な例

  2. Linuxシャットダウンコマンド:5つの実用的な例

  3. 10 実用的な Linux nm コマンドの例

  1. Linuxでのddコマンドの5つの実用例

  2. UNIX / Linux:10 の Netstat コマンドの例

  3. 10 Linux / UNIX での Xargs コマンドの例

  1. Linuxでの14のGrepコマンドの例

  2. Linuxでの「cd」コマンドの5つの実用例

  3. LinuxでのTracerouteコマンドの16の実用例