実世界で役立つ16のgrepコマンドの例

公開: 2021-02-23

もともと Unix ベースのシステム用に開発された grep は、Linux ボックスで最も広く使用されているコマンドライン ユーティリティの 1 つです。

その名前は、ed ツールの別の同様のコマンド、つまりg / re / pに由来します。これは、グローバルに正規表現を検索し、一致する行を出力することを表します。 基本的に grep は、標準入力またはファイルから特定のパターンまたは正規表現を検索し、特定の条件に一致する行を出力します。 多くの場合、大きなログ ファイルから必要な情報だけを出力する際に​​、不要な詳細を除外するために使用されます。

正規表現の力と grep でサポートされているオプションを組み合わせることで、これが可能になります。

ここでは、システム管理者または開発者がさまざまなシナリオで一般的に使用する grep コマンドの一部について説明します。

それでは始めましょう…

grep コマンドの構文

grep コマンドは、パイプなしで使用する場合、ファイル リストとともにパターンとオプションの引数を期待します。

 $ grep [options] pattern [files]

簡単な例は次のとおりです。

 $ grep my file.txt my_file $

複数のファイルを検索する

grep を使用すると、特定のパターンを 1 つだけでなく複数のファイルで検索できます。 *ワイルドカードを使用して、複数のファイルでパターンを検索する方法を次に示します。

 $ sudo grep -i err /var/log/messages*

出力:

 $ sudo grep err /var/log/messages* /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: Using IOAPIC for interrupt routing /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKA] (IRQs 5 9 10 *11) /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKB] (IRQs 5 9 *10 11) /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKC] (IRQs 5 *9 10 11) /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKD] (IRQs 5 9 10 *11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: Using IOAPIC for interrupt routing /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKA] (IRQs 5 9 10 *11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKB] (IRQs 5 9 *10 11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKC] (IRQs 5 *9 10 11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKD] (IRQs 5 9 10 *11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: BERT: Boot Error Record Table support is disabled. Enable it by using bert_enable as kernel parameter. /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKA] (IRQs 5 9 10 *11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKB] (IRQs 5 9 *10 11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKC] (IRQs 5 *9 10 11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKD] (IRQs 5 9 10 *11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: BERT: Boot Error Record Table support is disabled. Enable it by using bert_enable as kernel parameter. /var/log/messages-20201227:Dec 27 19:11:21 centos7vm kernel: [drm:vmw_host_log [vmwgfx]] *ERROR* Failed to send host log message. /var/log/messages-20201227:Dec 27 19:11:21 centos7vm kernel: [drm:vmw_host_log [vmwgfx]] *ERROR* Failed to send host log message. $

上記の出力から、一致する行を出力する前にファイル名が最初に出力され、 grepが特定のパターンを見つけた場所を示すことがわかります。

大文字と小文字を区別しない検索

grep は、パターンの大文字と小文字を区別せずにパターンを検索することを提案します。 -iフラグを使用して、grep に大文字と小文字を区別しないように指示します。

 $ grep -i [pattern] [file]

出力:

 $ grep -i it text_file.txt This is a sample text file. It contains functionality. You can always use grep with any kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $

全語検索

常に部分一致が必要なわけではなく、代わりに grep が完全な単語のみに一致することを期待しています。 -wフラグでそれを行うことができます。

 $ grep -w [pattern] [file]

出力:

 $ grep -w is text_file.txt This is a sample text file. It contains This is a sample text file. It's repeated two times. $

一致数を確認する

実際に一致した行の代わりに、grep が成功した一致の数だけが必要な場合があります。 -cオプションを使用して、このカウントを取得できます。

 $ grep -c [pattern] [file]

出力:

 $ grep -c is text_file.txt 2 $

サブディレクトリを検索

現在の作業ディレクトリだけでなく、サブディレクトリ内のファイルも検索する必要があることがよくあります。 grep を使用すると、 -rフラグを使用して簡単に実行できます。

 $ grep -r [pattern] *

出力:

 $ grep -r Hello * dir1/file1.txt:Hello One dir1/file2.txt:Hello Two dir1/file3.txt:Hello Three $

ご覧のとおり、grep は現在のディレクトリ内の各サブディレクトリを走査し、一致するファイルと行を一覧表示します。

逆検索

特定のパターンに一致しないものを見つけたい場合は、grep で-vフラグを使用してそれを行うことができます。

 $ grep -v [pattern] [file]

出力:

 $ grep This text_file.txt This is a sample text file. It contains This is a sample text file. It's repeated two times. $ grep -v This text_file.txt several lines to be used as part of testing grep functionality. You can always use grep with any kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. $

-vフラグを使用した場合と使用しない場合で、同じパターンとファイルに対するgrepコマンドの出力を比較できます。 -vを使用すると、パターンに一致しない行が印刷されます。

行番号を印刷する

grep を使用すると、印刷された行とともに行番号を印刷できるため、ファイル内の行がどこにあるかを簡単に知ることができます。 出力で行番号を取得するには、示されているように-nオプションを使用します。

 $ grep -n [pattern] [file]

出力:

 $ grep -n This text_file.txt 1:This is a sample text file. It contains 7:This is a sample text file. It's repeated two times. $

grep 出力を制限する

ログなどの大きなファイルの場合、grep の出力は長くなる可能性があり、すべてを一致させるのではなく、出力に一定数の行が必要な場合があります。 m[num]を使用して、印刷される行を num で制限できます。 使用方法は次のとおりです。

 $ grep -m[num] [pattern] [file]

以下の例で、 -mフラグを使用すると、同じ条件セットの grep の出力にどのように影響するかに注意してください。

 $ grep It text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $ grep -m2 It text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as $

追加の行を表示

多くの場合、一致するパターンを持つ行だけでなく、より良いコンテキストのためにその上または下の行が必要です。

-A-Bまたは-Cフラグとnum値を使用して、grep を使用してパターンを持つ行の上または下 (または両方) に行を印刷することができます。 ここでnumは、一致した行のすぐ上または下に出力される追加の行数を示します。 これは、指定したファイルまたはファイル リストで grep が検出したすべての一致に適用されます。

 $ grep -A[num] [pattern] [file]

また

$ grep -B[num] [pattern] [file]

また

$ grep -C[num] [pattern] [file]

以下の出力は、通常の grep 出力と、フラグ-A-B 、および-Cを 1 つずつ使用した出力を示しています。 grep がフラグとその値をどのように解釈するか、およびそれぞれの出力の変化に注目してください。 -A1フラグを指定すると、grep は一致する行の直後に続く 1 行を出力します。

同様に、 -B1フラグを使用すると、一致する行の直前に 1 行が出力されます。 -C1フラグを使用すると、一致する行の前後にある 1 行を出力します。

 $ grep numbers text_file.txt It supports numbers like 1, 2, 3 etc. as well as $ grep -A1 numbers text_file.txt It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. $ grep -B1 numbers text_file.txt kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as $ grep -C1 numbers text_file.txt kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. $

ファイル名の一覧表示

実際に一致した行ではなく、パターンが見つかったファイルの名前だけを出力するには、 -lフラグを使用します。

 $ grep -l [pattern] [file]

実行例を次に示します。

 $ grep -l su *.txt file.txt text_file.txt $

正確な行を印刷

場合によっては、特定のパターンの一部ではなく、正確に一致する行を印刷する必要があります。 grep を使用すると、 -xフラグでそれを行うことができます。

 $ grep -x [pattern] [file]

以下の例では、file.txt に「support」という単語が 1 つだけ含まれている行が含まれているため、「support」という単語と他のテキストを含む可能性のある行を無視しながら、 -xフラグを指定した grep によって照合されます。

 $ grep -x support *.txt file.txt:support $

マッチ開始文字列

正規表現を使用すると、行頭の文字列を見つけることができます。 方法は次のとおりです。

 $ grep [options] "^[string]" [file]

例:

 $ grep It text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $ grep ^It text_file.txt It supports numbers like 1, 2, 3 etc. as well as $

^文字を使用すると出力がどのように変化するかを観察してください。 ^は文字列の開始を示し、 grep は^It It単語で始まる任意の行として一致しました。 パターンにスペースなどが含まれている場合は、引用符で囲むと役立ちます。

一致終了文字列

もう 1 つの一般的な便利な正規表現は、行末パターンの一致です。

 $ grep [options] "[string]$" [file]

例:

 $ grep "\." text_file.txt This is a sample text file. It contains functionality. You can always use grep with any kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. This is a sample text file. It's repeated two times. $ grep "\.$" text_file.txt kind of data but it works best with text data. alphabets and special characters like - + * # etc. This is a sample text file. It's repeated two times. $

を一致させようとしました. 行末の文字。 ドット (.) は特別な意味を持つ文字であるため、 \文字でエスケープする必要があります。 マッチしただけで出力がどのように変化するかに注目して. $を使用して grep に.で終わる行のみに一致するように指示すると、 (その間のどこかにそれを含む可能性があるものではありません)。

パターンファイルを使用

頻繁に使用するパターンの複雑なリストがある場合があります。 毎回書き留める代わりに、ファイル内のパターンのリストを指定して、 -fフラグで使用できます。 ファイルには、1 行に 1 つのパターンが含まれている必要があります。

 $ grep -f [pattern_file] [file_to_match]

この例では、以下の内容でパターン ファイル名pattern.txtを作成しました。

 $ cat pattern.txt This It $

これを使用するには、 -fフラグを使用します。

 $ grep -f pattern.txt text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $

複数のパターンを指定する

grep では、 -eフラグを使用して複数のパターンを指定できます。

 $ grep -e [pattern1] -e [pattern2] -e [pattern3]...[file]

例:

 $ grep -e is -e It -e to text_file.txt This is a sample text file. It contains several lines to be used as part of testing grep It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $

拡張正規表現を指定する

grep は、 -Eフラグを使用して拡張正規表現または ERE もサポートします。 これは Linux のegrepコマンドに似ています。

ERE を使用すると、メタ文字をそのまま扱い、grep のように文字列として置き換えたくない場合に利点があります。 これにより、grep の場合に要求されるように、それらをエスケープするという点でより柔軟になります。 そうは言っても、 grep で-Eを使用することはegrepコマンドと同等です。

 $ grep -E '[Extended RegEx]' [file]

コメントや空白でない行を印刷したい場合の ERE の使用例を次に示します。 これは、大きな構成ファイルで何かを見つけるのに特に役立ちます。 さらに、パターン'^(#|$)'に一致する行を出力しないように-vフラグを使用しました。

 $ sudo grep -vE '^(#|$)' /etc/ssh/sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server $

結論

上記の例は氷山の一角にすぎません。 grep はさまざまなオプションをサポートしており、効果的な使い方を知っている人にとっては非常に便利なツールです。 上記の例を使用するだけでなく、それらをさまざまな方法で組み合わせて、必要なものを取得できます。

詳細については、man ページを参照してください。

 $ man grep

次に、SFTP コマンドの例を学びます。