ホームページ  >  記事  >  php教程  >  Linux の基本的なコマンド テキスト フィルタリング grep

Linux の基本的なコマンド テキスト フィルタリング grep

高洛峰
高洛峰オリジナル
2016-11-08 14:49:381152ブラウズ

Linux ではテキストや出力コンテンツをフィルタリングする必要があることがよくあります。最も一般的に使用されるフィルタリング コマンドは grep です。

grep [OPTIONS] PATTERN [FILE...]

grep は、入力行にパターン PATTERN が含まれている場合、この行を 1 行ずつ取得します。ここでの PATTERN は正規表現です (前の記事を参照してください。この記事では例として grep を使用します)。

root を含む行をファイル /etc/passwd に出力します:

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

または、標準入力から取得します:

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

注意する必要があるのは、grep の入力がファイルと標準入力の両方から来る場合、grep は標準入力を無視する 標準入力を表すために記号 - が使用されない限り、コンテンツは処理されません:

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

この時点で、grep はどの結果がファイルからのもので、どの結果が標準入力からのものかを示します。

ファイル /etc/passwd およびファイル /etc/group 内の root で始まる行を出力します:

[root@centos7 temp]# cat /etc/passwd | grep root /etc/passwd -
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
(标准输入):root:x:0:0:root:/root:/bin/bash
(标准输入):operator:x:11:0:operator:/root:/sbin/nologin

この時点で、grep はどの結果がファイルからのもので、どの結果が標準入力からのものかを示します。

ファイル /etc/passwd およびファイル /etc/group の root で始まる行を出力します:

[root@centos7 temp]# grep "^root" /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/group:root:x:0:

ファイル /etc/passwd の /bin/bash で終わる行を出力します:

[root@centos7 temp]# grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
learner:x:1000:1000::/home/learner:/bin/bash

のパターンに注意してください上記の 2 つの例は、シェルによる解析を防ぐために二重引用符で囲まれています。

ファイル /etc/passwd 内の a-s の文字で始まらない行を出力します:

[root@centos7 temp]# grep "^[^a-s]" /etc/passwd 
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin

ここで、最初の ^ は行の先頭を表し、 2 番目の文字は [] 内にあります。最初の文字 ^ は否定を意味します。

ファイル /etc/passwd 内で、文字 0 が 3 回以上連続して出現する行を出力します (エスケープ文字 '' に注意してください):

[root@centos7 temp]# grep "0\{3,\}" /etc/passwd
learner:x:1000:1000::/home/learner:/bin/bash

たとえば、文字 r または l で始まる行を出力します。ファイル /etc/passwd:

[root@centos7 temp]# grep "^[r,l]" /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
learner:x:1000:1000::/home/learner:/bin/bash

オプション -i は、パターンに一致するときに grep に大文字と小文字を無視させます:

[root@centos7 temp]# grep -i abcd file 
ABCD
function abcd() {
[root@centos7 temp]#

オプション -o は、行全体ではなく、一致する文字のみを出力することを意味します:

[root@centos7 temp]# grep -oi abcd file 
ABCD
abcd
[root@centos7 temp]#

オプション -c は、次の文字の数をカウントします。一致する行:

[root@centos7 temp]# grep -oic abcd file 
2
[root@centos7 temp]#

オプション -v /etc/passwd 内の /sbin/nologin で終わらない行を出力するなど、逆一致を示します:

[root@centos7 temp]# grep -v "/sbin/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
learner:x:1000:1000::/home/learner:/bin/bash

オプション -f FILE は、ファイル FILE 内の各行をパターンとして使用することを意味しますmatch:

[root@centos7 temp]# cat test
abcd
ABCD
[root@centos7 temp]# grep -f test file 
ABCD
function abcd() {
[root@centos7 temp]#

Option -x は行全体を意味します match:

[root@centos7 temp]# grep -xf test file 
ABCD
[root@centos7 temp]#

Option-w は単語全体と一致することを意味します:

[root@centos7 temp]# grep here file
here
there
[root@centos7 temp]# grep -w here file
here
[root@centos7 temp]#

Option-h は複数のファイルがある場合にファイル名を出力しないことを意味します:

[root@centos7 temp]# cat /etc/passwd|grep ^root - /etc/passwd -h
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash

Option -n は行番号を表示することを意味します:

[root@centos7 temp]# grep -n "^[r,l]" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
24:learner:x:1000:1000::/home/learner:/bin/bash

オプション-A N、-B N、-C N は一致する行とその「周囲の行」を出力することを意味します

-A N 表示输出匹配行和其之后(after)的N行
-B N 表示输出匹配行和其之前(before)的N行
-C N 表示输出匹配行和其之前之后各N行
[root@centos7 temp]# grep -A 2 ^operator /etc/passwd
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@centos7 temp]# grep -B2 ^operator /etc/passwd   
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
[root@centos7 temp]# grep -C1 ^operator /etc/passwd  
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

オプション -F は PATTERN をリテラル一致として扱います (パターンの特別な意味は無視します)これは、コマンド fgrep:

[root@centos7 temp]# grep -F ^root /etc/passwd
[root@centos7 temp]#

を実行するのと同等です

[root@centos7 temp]# egrep "^root|^learner" /etc/passwd
root:x:0:0:root:/root:/bin/bash
learner:x:1000:1000::/home/learner:/bin/bash

このコマンドには出力がありません

オプション -E は、egrep コマンドを実行するのと同じように、拡張正規表現を使用できます:

[root@centos7 ~]# echo "helloworld123456"| grep -oP "\d+"
123456
[root@centos7 ~]#

拡張正規表現を使用するということは、次の特別な意味を意味します?、+、{、|、(and) などの文字はエスケープせずに表現できます。

オプション -P は、マッチングに Perl の正規表現を使用することを意味します

例:

[root@centos7 ~]# grep -a online /usr/bin/ls
%s online help: <%s>
[root@centos7 ~]#

Perl 正規表現の「d」は数値を意味し、+ は 1 回から複数回のマッチングを意味します (vim と同じ)。

オプション -a はバイナリ ファイルをテキスト ファイルとして扱います:

[root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash
./test.sh:#!/bin/bash
[root@centos7 temp]#

オプション --exclude=GLOB と --include=GLOB は、それぞれ GLOB に一致するファイルを除外および含めることを意味し、GLOB はワイルドカードを意味します (find と xargs の基本的なコマンドの紹介を参照してください)使用法) 3):

rrreee🎜grep の強力なフィルタリング機能は、さまざまなオプションと正規表現の組み合わせから生まれます🎜
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。