ホームページ  >  記事  >  php教程  >  Grep コマンド 9 の古典的な使用シナリオの詳細な説明

Grep コマンド 9 の古典的な使用シナリオの詳細な説明

高洛峰
高洛峰オリジナル
2016-12-13 14:54:101663ブラウズ

Grep
正式名称は Global Regular Expression Print で、グローバル正規表現を意味します
正規表現を使用した強力なテキスト検索ツールです
1. コマンド形式
grep [オプション] ファイル


2. : 出力のみ 一致する行数
-i: 大文字と小文字を区別しない
-n: 一致するナビゲーションと行番号を表示する
-l: 複数のファイルをクエリする場合、一致する文字を含むファイル名のみを出力する
-v: 逆一致、つまり一致する行は表示されません
-h: クエリ時にファイル名は適用されません
-s: エラー メッセージは表示されません


3. 正規表現の一部
反意文字: """ などは "" と一致することを意味します
^$ 開始と終了
[] 単一文字、[A]
[-] は範囲に一致、[0-9a-zA-Z] はすべての数字と文字に一致
* 前の文字は 0 回以上出現します
+ 前の文字文字が 1 回以上表示されます
。任意の文字


4. クラシックなシーン

大文字と小文字を区別したくない場合は、大文字と小文字を区別しないように -i を追加してください

(1) find コマンドとパイプを組み合わせます

次のいずれかあなたの音楽 フォルダー内にはさまざまな形式のファイルがありますが、アーティスト Jay の MP3 ファイルを見つけたいだけであり、混合トラックは含まれていません
[root@localhost ~]#find -name ".mp3" 。 | grep -i jay | grep -vi "remix"
分析: 1) find -name を使用してすべての mp3 ファイルをリストし、grep にリダイレクトします
2) grep -i を使用して jay を含む行を検索します
3) grep -vi を使用してリミックスを含む行


(2)-A -B -C
多くの場合、私たちは行の一致ではなく、行の一致のコンテキストを気にします。この時、-A -B -C が便利です
-A n n 行後、A は (After) と記憶されます
-B n n 行前、B は(Before) と記憶されます
-C n n 行前、n 行後, Cメモリは(中央)

[root@localhost ~]# ifconfig | grep -A 2 "Link encap"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F3:38:15  
          inet addr:192.168.91.129  Bcast:192.168.91.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link
--
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host


[root@localhost ~]#  ifconfig | grep -C 2 "lo"
          Interrupt:67 Base address:0x2024 


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

(3) -cを使って数を数えます

手元に大きなファイルがあります。このファイルにはwww.baidu.comtieba.baidu.comなどのURLが含まれています。の上。 Baidu に属する URL の数を知りたいですか?ここのドットは現在のディレクトリを表します

[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* .


例:

root@localhost ~]# grep -c "*baidu.com*" filename
例子
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -cn ".*baidu.com.*" file.txt 
3

サブディレクトリを検索し、一致したファイル名のみを出力します

[root@localhost ~]# grep -lr HELLO_HWC_CSND_BLOG* .

例:

[root@localhost ~]# grep -nr baidu .
./file.txt:8:www.baidu.com
./file.txt:9:tieba.baidu.com
./file.txt:11:www.baidu.com/search/index
./test/test.txt:1:http://www.baidu.com

(5)--line-buffered バッファリング モードをオンにします
動的ファイルがあり、常にファイルの末尾に情報が追加され、出力が必要です。特定の情報を含めてもOKです。つまり、動的ストリームを継続的に grep します

[root@localhost ~]#tail -f file | grep --line-buffered your_pattern


(6) ps と組み合わせてプロセスを見つけます

[root@localhost ~]# grep -lr baidu .
./file.txt
./test/test.txt

ここでは grep を示しますinit us 実行したコマンドも一覧表示されます
この行が不要な場合は、次のようにコマンドを変更できます
[root@localhost ~]# ps aux | grep init
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root      4956  0.0  0.1   3920   680 pts/1    R+   23:27   0:00 grep init

(7) 特定のディレクトリを含まないディレクトリを検索
[root@localhost ~]#grep -R --exclude-dir=node_modules 'some pattern' /path/to/search


[root@localhost ~]# ps aux | grep [i]nit
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients

この時点でテストディレクトリを含めたくない場合

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[root@localhost ~]# grep -r baidu .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
./test/test.txt:http://www.baidu.com

エラーが報告された場合

[root@localhost ~]# grep -R --exclude-dir=text "baidu" .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index

それはバージョンが古すぎることを意味します、更新してください

(8) IP アドレスを見つけます

ここでは -o と -P コマンドが使用されます

man grep を通してチェックします

-o, --only- matching:
PATTERN に一致する行の一部のみを表示します。

-P, --perl- regexp:

PATTERN を Perl 正規表現として解釈します
つまり、-o、一致した行の部分のみを表示します。 Perl の正規一致として正規表現
-P と一致します

grep: unrecognized option `--exclude-dir=test'

(9) メールボックスを検索します

[root @localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a -zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+" ファイル。txt

example

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。