LoginSignup
0
0

Linux - GREP

Posted at

grepの使い方

概要

grep(Global search REgular expression and Print out the line)は、ユーザーが指定した「パターン」に基づいて目的のテキストを行ごとにマッチングチェックし、マッチした行を表示するテキスト検索ツールです。このパターンは、正規表現の文字とテキスト文字で記述されたフィルタ条件です。

詳しい使い方は以下のマニュアルを参照してください:
man7.org/linux/man-pages/man1/grep.1.html

使用方法

grep [OPTIONS] PATTERN [FILE...]

よく使われるオプション:

  • -E|--extended-regexp # EREを使用する、egrepと同じ
  • -F|--fixed-strings # 正規表現をサポートしない、fgrepと同じ
  • -G|--basic-regexp # スタイルを基本の正規表現として扱う
  • -P|--perl-regexp # Perl形式の正規表現をサポート
  • -e|--regexp=PATTERN # 複数のオプション間の論理的OR関係を実現, 例: grep -e 'cat' -e 'dog' file
  • -f|--file=FILE # ファイルからマッチングルールを読み込む、各行に一つ
  • -i|--ignore-case # 大文字と小文字の区別をしない
  • -w|--word-regexp # 単語全体にマッチする
  • -x|--line-regexp # 行全体にマッチする
  • -s|--no-messages # エラーメッセージを表示しない
  • -v|--invert-match # マッチしなかった行を表示する、逆マッチ
  • -B|--before-context=N # マッチした文字列がある行とその前N行を表示
  • -A|--after-context=N # マッチした文字列がある行とその後N行を表示
  • -C|--context=N|--context=N # マッチした文字列がある行とその前後N行を表示
  • --color=auto # マッチした内容をハイライト表示[always|never|auto]
  • -m|--max-count=N # 最初のN行のみマッチする
  • -b|--byte-offset # マッチした行の最初の文字の位置を表示
  • -n|--line-number # マッチした行の行番号を表示
  • -H|--with-filename # マッチした行が存在するファイル名を表示
  • -h|--no-filename # マッチした行が存在するファイル名を表示しない
  • -o|--only-matching # マッチした文字列のみを表示
  • -q|--quiet|--silent # 静かなモード、結果は変数$0から取得
  • --binary-files=TYPE # バイナリファイルの取り扱い方法指定 [binary|text|without-match]
  • -a|--text # --binary-files=textと同じ
  • -I # --binary-files=without-matchと同じ
  • -d|--directories=ACTION # ディレクトリの探索方法 [read|recurse|skip]
  • -D|--devices=ACTION # デバイスファイルの取り扱い方法 [read|skip]
  • -r|--recursive # ディレクトリ内を再帰的に検索、シンボリックリンクは処理しない
  • -R|--dereference-recursive # ディレクトリ内を再帰的に検索、シンボリックリンクを処理
  • -L|--files-without-match # マッチしなかったファイルの名前のみ表示
  • -l|--files-with-matches # マッチしたファイルの名前のみ表示
  • -c|--count # マッチした行の数を集計

例:

標準入力からの検索

[root@rocky86 ~]# grep hello
123hello456
123hello456
[root@rocky86 ~]# grep hello -
123hello456 
123hello456

標準入力からhelloという文字列を含む行を検索します。

ファイル処理

[root@rocky86 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

/etc/passwdファイル内でrootという文字列を含む行を検索します。

パイプを通じた検索

[root@rocky86 ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

catコマンドで表示した/etc/passwdの内容からrootという文字列を含む行を検索します。

入力リダイレクションを使用した検索

[root@rocky86 ~]# grep root < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

/etc/passwdファイルからrootを検索します。入力はファイルリダイレクションを通じて提供されます。

マッチする最初の3行を表示

[root@rocky86 ~]# grep -m 3 bin /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

/etc/passwdファイルからbinを含む最初の3行を検索します。

反転して、一致しない行を取得する

[root@rocky86 ~]# grep -v nologin /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
jose:x:1010:1010::/home/jose:/bin/bash
jerry:x:1011:1011::/home/jerry:/bin/bash
mage:x:1000:1000::/home/mage:/bin/bash

#コメント行を無視
[root@rocky86 ~]# grep -v "#" /etc/fstab 

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

[root@rocky86 ~]# grep -i ROOt /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

行番号を表示

[root@rocky86 ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

行に一致する数を表示

[root@rocky86 ~]# grep -c root /etc/passwd
2

一致した内容のみを表示

[root@rocky86 ~]# grep -o root /etc/passwd
root
root
root
root

サイレントモード

[root@rocky86 ~]# grep -q root /etc/passwd
[root@rocky86 ~]# echo $?
0
[root@rocky86 ~]# grep -q roottttttt /etc/passwd
[root@rocky86 ~]# echo $?
1

表示一致した行とその後の2行

[root@rocky86 ~]# grep -A 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

表示一致した行とその前の2行

[root@rocky86 ~]# grep -B 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

表示一致した行とその前後の各2行

[root@rocky86 ~]# grep -C 2 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

表示「root」と一致する行または「bash」と一致する行

[root@rocky86 ~]# grep -e root -e bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
jose:x:1010:1010::/home/jose:/bin/bash
jerry:x:1011:1011::/home/jerry:/bin/bash
mage:x:1000:1000::/home/mage:/bin/bash

表示「root」とも「bash」とも一致する行

[root@rocky86 ~]# grep root /etc/passwd | grep bash
root:x:0:0:root:/root:/bin/bash

ファイルから一致する規則を読み取る

[root@rocky86 ~]# cat test.txt 
root
bash
[root@rocky86 ~]# grep -f test.txt /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
jose:x:1010:1010::/home/jose:/bin/bash
jerry:x:1011:1011::/home/jerry:/bin/bash
mage:x:1000:1000::/home/mage:/bin/bash

再帰的なマッチング

#リンクを処理しない
[root@rocky86 ~]# grep -r root /etc/*
#リンクを処理する
[root@rocky86 ~]# grep -R root /etc/*

特定の条件に一致するファイル名のみを表示し、詳細な内容は表示しない

[root@rocky86 ~]# grep root -l /etc/passwd /etc/sudoers /etc/my.cnf /etc/issue
/etc/passwd
/etc/sudoers

特定の内容がどのファイルから来たかを表示する

[root@rocky86 ~]# grep -H root /etc/passwd /etc/sudoers /etc/my.cnf /etc/issue
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/sudoers:## the root user, without needing the root password.
/etc/sudoers:## Allow root to run any commands anywhere 
/etc/sudoers:root ALL=(ALL) ALL
/etc/sudoers:## cdrom as root

コマンドライン展開

[root@rocky86 ~]# grep `whoami` /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@rocky86 ~]# grep $(whoami) /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@rocky86 ~]# echo Linux123 | grep $(uname) 
Linux123

変数展開

[root@rocky86 ~]# grep "$USER" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

CPUコア数を取得

[root@rocky86 ~]# grep -c processor /proc/cpuinfo
2

パーティションの利用率の最大値

[root@centos8 ~]#df | grep '^/dev/sd' |tr -s ' ' %|cut -d% -f5|sort -n|tail -1
[root@centos8 ~]#df |grep '^/dev/sd' |grep -oE '\<[0-9]{,3}%'|tr -d '%'|sort -nr|head -n1
[root@centos8 ~]#df |grep '^/dev/sd' |grep -oE '\<[0-9]{,3}%'|grep -Eo '[0-9]+' |sort -nr|head -n1
13

現在のホストに最も多く接続している上位3つのIPアドレスを取得する

[root@centos8 ~]#ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n3
      3 10.0.0.1
      1 172.16.4.100
      1 172.16.31.188

接続状態の統計

[root@wang-liyun-pc ~]# ss -nta | grep -v '^State' |cut -d" " -f1|sort |uniq -c
      7 ESTAB
      4 LISTEN
      7 TIME-WAIT     
[root@wang-liyun-pc ~]# ss -nta | tail -n +2 |cut -d" " -f1|sort |uniq -c
      3 ESTAB
      4 LISTEN
     12 TIME-WAIT

ファイルのコメント (# 記号の付いた行を含む) と空白行をフィルターで除外

[root@centos8 ~]#grep -Ev '^$|#' /etc/fstab 
UUID=01f1068e-6937-4fb2-b64b-0d7d6b85ad08 /           xfs     defaults        0
0
UUID=cb21e5ce-edf6-4ed1-8df9-ba98520a68dc /boot       xfs     defaults        0
0
UUID=9ea3524a-7cff-49a0-951e-8429a30bd0a0 /data       xfs     defaults        0
0
UUID=42174d44-41aa-448b-88bc-fd36d6a49e39 swap         swap   defaults        0
0

注意:

上記の例は、grepコマンドの基本的な使用方法を示しています。オプションや引数を変更することで、様々な検索条件や出力フォーマットを指定することができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0