grep 常用選項講解

轉(zhuǎn)自:https://www.cnblogs.com/tlnshuju/p/7106790.html

1. grep最簡單的用法,匹配一個詞:grep word filename

2. 能夠從多個文件里匹配:grep word filename1 filenam2 filename3

3. 能夠使用正則表達式匹配:grep -E pattern f1 f2 f3...

4. 能夠使用-o僅僅打印匹配的字符,例如以下所看到的:

lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\."

line.

5. 打印除匹配行之外的其它行风罩,使用-v

lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"

3

4

6. 統(tǒng)計匹配字符串的行數(shù)凝颇。使用-c

lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c

2

7. 假設(shè)我們統(tǒng)計字符串模式匹配的次數(shù)。能夠結(jié)合-o和-c。例如以下:

lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]"? | wc -l

8

8. 假設(shè)須要顯示行號快压,能夠打開-n拯田,例如以下:

lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"

3:33333

9. -b選項能夠打印出匹配的字符串想對于其所在的行起始位置的偏移量(從0開始)历造。通常配合-o使用,例如以下:

lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4

4:4

10. 當(dāng)字符串在多個文件里匹配時船庇。-l選項將僅僅打印文件名稱

11. -L與-l相對吭产。僅僅打印不匹配的文件名稱

lichao@ubuntu:command$ cat test1.txt

linux

is

fun

lichao@ubuntu:command$ cat test2.txt

a

very

popular

os,

linux

lichao@ubuntu:command$ cat test3.txt

what

the

fxxk

lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt

test1.txt

test2.txt

lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt

test3.txt

12. 打開遞歸搜索功能

lichao@ubuntu:command$ grep -n -R linux .

./test2.txt:5:linux

./test1.txt:1:linux

13. 忽略大寫和小寫:-i

lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello"

HELLO WORLD

14. 匹配多個字符串模式

lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o

This

is

line

15. 用單獨的文件提供匹配樣式,每一個匹配的樣式作為一行鸭轮,例如以下例所看到的:

lichao@ubuntu:command$ cat pattern.txt

1$

2

3

lichao@ubuntu:command$ cat num.txt

1

2

3

4

5

6

7

8

9

10

lichao@ubuntu:command$ grep -f pattern.txt num.txt

1

2

3

16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息臣淤。使用-B n打印匹配行及其前n行信息。使用 -C n窃爷。打印匹配行及其前后n行信息邑蒋。假設(shè)有多重匹配姓蜂,將使用--隔離。

示比例如以下:

lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3

5

6

7

8

lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3

2

3

4

5

lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3

2

3

4

5

6

7

8

lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2

a

b

c

--

a

b

c

17. 使用-q進入靜默模式寺董,該模式下覆糟。grep命令執(zhí)行目的不過執(zhí)行一個條件測試。通常在腳本中使用遮咖。通過檢查其返回值進行下一步操作滩字。示比例如以下:

lichao@ubuntu:command$ cat tmp.txthelloworldlichao@ubuntu:command$ cat tmp.csh#!/bin/bashif [ $# -ne 2 ]; thenecho "Usage: $0 match_pattern file_name"exitfimatch=$1file=$2grep -q $match $fileif [ $?

-ne 0 ]; then

echo "$match not exist in $file"

else

echo "$match exist in $file"

fi

lichao@ubuntu:command$ ./tmp.csh hello tmp.txt

hello exist in tmp.txt

18. -Z選項在輸出匹配文件名稱時將以/0結(jié)尾配合xargs -0能夠發(fā)揮非常多作用,比如刪除匹配某個模式的文件例如以下:

lichao@ubuntu:command$ ls -llrt

total 28

-rw-rw-r-- 1 lichao lichao? 13 Nov? 1 20:38 test1.txt

-rw-rw-r-- 1 lichao lichao? 27 Nov? 1 20:39 test2.txt

-rw-rw-r-- 1 lichao lichao? 14 Nov? 1 20:39 test3.txt

-rw-rw-r-- 1 lichao lichao? 21 Nov? 1 20:45 num.txt

-rw-rw-r-- 1 lichao lichao? 7 Nov? 1 20:45 pattern.txt

-rw-rw-r-- 1 lichao lichao? 12 Nov? 1 21:25 tmp.txt

-rwxr-xr-x 1 lichao lichao 217 Nov? 1 21:27 tmp.csh

lichao@ubuntu:command$ cat test1.txt

linux

is

fun

lichao@ubuntu:command$ cat test2.txt

a

very

popular

os,

linux

lichao@ubuntu:command$ grep "linux" * -lZ | xargs -0 rm

lichao@ubuntu:command$ ls

num.txt? pattern.txt? test3.txt? tmp.csh? tmp.txt

以上命令將包括linux字符串的test1.txt和test2.txt刪除御吞。

19. 排除/包含文件或者文件夾:1)--include *{.c,.cpp} 僅僅在文件夾中搜索.c和.cpp文件麦箍;2)--exclude "README" 排除全部README文件 3) --include-dir 僅在某些文件夾中搜索 4) --exclude-dir 排除某些文件夾 5) --exclude-from FILE 從文件FILE中讀取須要排除的文件列表

lichao@ubuntu:test$ ls

dir1? dir2? exclude.config? test1.txt? test2.doc? test3.word

lichao@ubuntu:test$ cat test1.txt

linux

is

fun

lichao@ubuntu:test$ cat test2.doc

wonderful

os,

linux

lichao@ubuntu:test$ cat test3.word

wonderful

os,

linux

lichao@ubuntu:test$ ls dir1/

test1.txt? test2.doc? test3.word

lichao@ubuntu:test$ ls dir2/

test1.txt? test2.doc? test3.word

lichao@ubuntu:test$ cat exclude.config

*.txt

lichao@ubuntu:test$ grep "linux" -R -n .

./test2.doc:3:linux

./test3.word:3:linux

./test1.txt:1:linux

./dir2/test2.doc:3:linux

./dir2/test3.word:3:linux

./dir2/test1.txt:1:linux

./dir1/test2.doc:3:linux

./dir1/test3.word:3:linux

./dir1/test1.txt:1:linux

lichao@ubuntu:test$ grep "linux" -R -n . --include *.txt --include *.doc

./test2.doc:3:linux

./test1.txt:1:linux

./dir2/test2.doc:3:linux

./dir2/test1.txt:1:linux

./dir1/test2.doc:3:linux

./dir1/test1.txt:1:linux

lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc

grep: unrecognized option '--eclude'

Usage: grep [OPTION]... PATTERN [FILE]...

Try 'grep --help' for more information.

lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc

./test3.word:3:linux

./dir2/test3.word:3:linux

./dir1/test3.word:3:linux

lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1

./test2.doc:3:linux

./test3.word:3:linux

./test1.txt:1:linux

./dir2/test2.doc:3:linux

./dir2/test3.word:3:linux

./dir2/test1.txt:1:linux

lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2

./test2.doc:3:linux

./test3.word:3:linux

./test1.txt:1:linux

lichao@ubuntu:test$ grep "linux" -R -n . --exclude-from exclude.config

./test2.doc:3:linux

./test3.word:3:linux

./dir2/test2.doc:3:linux

./dir2/test3.word:3:linux

./dir1/test2.doc:3:linux

./dir1/test3.word:3:linux

已上即為grep經(jīng)常使用的選項。

注意:轉(zhuǎn)載請注明出處陶珠。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末挟裂,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子揍诽,更是在濱河造成了極大的恐慌诀蓉,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暑脆,死亡現(xiàn)場離奇詭異渠啤,居然都是意外死亡,警方通過查閱死者的電腦和手機添吗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門沥曹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人碟联,你說我怎么就攤上這事妓美。” “怎么了鲤孵?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵壶栋,是天一觀的道長。 經(jīng)常有香客問我普监,道長委刘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任鹰椒,我火速辦了婚禮,結(jié)果婚禮上呕童,老公的妹妹穿的比我還像新娘漆际。我一直安慰自己,他們只是感情好夺饲,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布奸汇。 她就那樣靜靜地躺著施符,像睡著了一般。 火紅的嫁衣襯著肌膚如雪擂找。 梳的紋絲不亂的頭發(fā)上戳吝,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音贯涎,去河邊找鬼听哭。 笑死,一個胖子當(dāng)著我的面吹牛塘雳,可吹牛的內(nèi)容都是我干的陆盘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼败明,長吁一口氣:“原來是場噩夢啊……” “哼隘马!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起妻顶,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤酸员,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后讳嘱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體幔嗦,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年呢燥,在試婚紗的時候發(fā)現(xiàn)自己被綠了崭添。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡叛氨,死狀恐怖呼渣,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情寞埠,我是刑警寧澤屁置,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站仁连,受9級特大地震影響蓝角,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饭冬,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一使鹅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧昌抠,春花似錦患朱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽冰沙。三九已至,卻和暖如春执虹,著一層夾襖步出監(jiān)牢的瞬間拓挥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工袋励, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留侥啤,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓插龄,卻偏偏與公主長得像愿棋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子均牢,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容