Linuxコマンド紹介-「grep」


この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので十分ご注意ください。

これからLinuxの世界でよく使われるコマンドを紹介しようと思います。

まずはsed、awk、grep コマンド:

先日はsedを紹介しましたので、本日はgrepです。
grepを使って文字列を全置換したり、行単位で抽出したり、削除したり、いろいろなテキスト処理のできるコマンド。

1.名前grepの由来

grep の名の由来は、ラインエディタedのコマンド g/re/p である。その意味するところは「global regular expression print(ファイル全体から/正規表現に一致する行を/表示する)」で略号になっている

2.基本文法

grep コマンドの基本的な使い方は

grep オプション パターン ファイル

3.便利なオプションたち

いくつかの簡単なケース操作の後、grepの一般的な使用法に精通しているはずです。grepの一般的なオプションを簡単に要約しましょう。実際の練習後の要約は、より良い学習効果があるはずです。

  • -A:一致する行に加えて、行ののN行が追加で表示する

  • -B:一致する行に加えて、行ののN行が追加で表示する

  • -C:一致する行に加えて、行の前後のN行が追加で表示する

  • -c:一致した行を数える

  • -e:複数のオプション間のロジック or の意味

  • -E:拡張正規表現をサポートする

  • -F:fgrepと同等

  • -i:アルファベットの大文字小文字の区別をしない。

  • -n:一致する行番号を表示する

  • -o:パターンに一致した箇所のみ出力する。

  • -q:クワイエットモード、情報は出力されません。スクリプトで一般的に使用される

  • -s:エラーメッセージを表示しない

  • -v:パターンに一致しない行を出力する。

  • -w:単語全体を表示する

  • -color:一致した文字列を色で強調表示する

4.使用例

1:指定されたプロセスを見つける

コマンド:

ps -ef|grep svn

アウトプット:

[root@localhost ~]# ps -ef|grep svn
root 4943  1   0 Dec05 ?  00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0  00:00:00 grep svn
[root@localhost ~]#

説明:
最初のレコードは検出されたプロセスです。2番目の結果はgrepプロセス自体であり、検出される実際のプロセスではありません。

2:指定されたプロセスの数を見つける

コマンド:

ps -ef|grep svn -c
ps -ef|grep -c svn

アウトプット:

[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn 
2
[root@localhost ~]#

3:ファイルからキーワードを読み取って検索する

コマンド:

cat test.txt | grep -f test2.txt

アウトプット:

[root@localhost test]# cat test.txt 
hnlinux
cloud5.jp
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt 
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#

説明:test2.txtファイルから読み取ったキーワードを含むコンテンツ行をtest.txtファイルに出力します

4:ファイルからキーワードを読み取り、行番号を検索して表示する

コマンド:

cat test.txt | grep -nf test2.txt

アウトプット:

[root@localhost test]# cat test.txt 
hnlinux
cloud5.jp
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt 
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#

説明:test2.txtファイルから読み取ったキーワードを含むtest.txtファイルのコンテンツ行を出力し、各行の行番号を表示します

5:ファイルからキーワードを検索する

コマンド:

grep 'linux' test.txt

アウトプット:

[root@localhost test]# grep 'linux' test.txt 
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt 
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#

6:複数のファイルからキーワードを検索する

コマンド:

grep 'linux' test.txt test2.txt

アウトプット:

[root@localhost test]# grep -n 'linux' test.txt test2.txt 
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt 
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#

説明:複数のファイルがある場合、照会した情報コンテンツ行を出力する場合、ファイル名が行の先頭に出力され、識別子として「:」が追加されます。

7:grepは独自のプロセスを表示しません

コマンド:

ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"

アウトプット:

[root@localhost test]# ps aux|grep ssh
root  2720 0.0 0.0 62656 1212 ?   Ss  Nov02  0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ?   Ss  19:53  0:00 sshd: root@pts/0 
root 16901 0.0 0.0 61180  764 pts/0 S+  20:31  0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root  2720 0.0 0.0 62656 1212 ?   Ss  Nov02  0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ?   Ss  19:53  0:00 sshd: root@pts/0 
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root  2720 0.0 0.0 62656 1212 ?   Ss  Nov02  0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ?   Ss  19:53  0:00 sshd: root@pts/0

8:uで始まる行の内容を検索する

コマンド:

cat test.txt |grep ^u

アウトプット:

[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#

9:uで始まらない行の内容を出力する

コマンド:

cat test.txt |grep ^[^u]

アウトプット:

[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
cloud5.jp
redhat
Redhat
linuxmint
[root@localhost test]#

10:「hat」で終わる行コンテンツを出力する

コマンド:

cat test.txt |grep hat$

アウトプット:

[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#

最後に

以上です。
どなたかの参考になれば幸いです〜。

Last modified: 2022-07-12

Author