国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

Linux 命令

2024-06-28 13:20:59
字體:
供稿:網(wǎng)友
linux 命令 - grep: 正則搜索文本

grep 搜索文本文件中與指定正則表達式匹配的行

命令格式

grep [OPTIONS] PATTERN [FILE...]

命令參數(shù)

Generic PRogram Information

--help  打印幫助信息

-V, --version  打印版本信息

Matcher Selection

-E, --extended-regexp  使用基本正則表達式(ERE)解釋 PATTERN

-F, --fixed-strings  每個模式作為一組固定字符串對待(以新行分隔),而不作為正則表達式。

-G, --basic-regexp  使用基本正則表達式(BRE)解釋 PATTERN

-P, --perl-regexp  使用 Perl 正則表達式(PCRE)解釋 PATTERN

Matching Control

-e PATTERN, --regexp=PATTERN  使用 PATTERN 作為匹配模式

-f FILE, --file=FILE  從文件獲取匹配模式

-i, --ignore-case  忽略大小寫

-v, --invert-match  輸出不含匹配項的行

-w, --Word-regexp  單詞精確匹配

-x, --line-regexp  行精確匹配

General Output Control

-c, --count  輸出匹配項的數(shù)目

-l, --files-with-matches  輸出包含匹配項的文件名而不是直接輸出匹配行

-L, --files-without-match  與 -l 選項類似,輸出的是包含不匹配項的文件名

-m NUM, --max-count=NUM  當(dāng)匹配 NUM 后停止讀取文件

-o, --only-matching  只顯示匹配項而不是匹配行

-q, --quiet, --silent  安靜模式,不顯示任何信息

-s, --no-messages  當(dāng)文件不存在或不可讀時,不顯示錯誤信息

Output Line Prefix Control

-b, --byte-offset  在每個匹配行前加上該行在文件內(nèi)的塊號

-H, --with-filename  在每個匹配行前加上文件名

-h, --no-filename  多文件搜索時,抑制文件名輸出

-n, --line-number  在每個匹配行前加上該行在文件內(nèi)的行號

Context Line Control

-A NUM, --after-context=NUM  輸出匹配行及其后 NUM 行的內(nèi)容

-B NUM, --before-context=NUM  輸出匹配行及其前 NUM 行的內(nèi)容

-C NUM, -NUM, --context=NUM  輸出匹配行及其前后 NUM 行的內(nèi)容

實例

測試文件 test.txt:

The Zen of Python, by Tim PetersBeautiful is better than uglyExplicit is better than implicitSimple is better than complexComplex is better than complicatedFlat is better than nestedSparse is better than denseReadability countsSpecial cases aren't special enough to break the rulesAlthough practicality beats purityErrors should never pass silentlyUnless explicitly silencedIn the face of ambiguity, refuse the temptation to guessThere should be one-- and preferably only one --obvious way to do itAlthough that way may not be obvious at first unless you're DutchNow is better than neverAlthough never is often better than *right* nowIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good ideaNamespaces are one honking great idea -- let's do more of those

a) 輸出含有 "com" 的行

huey@huey-K42JE:~/huey/linux/cmdline$ grep com test.txtSimple is better than complexComplex is better than complicated

b) 匹配時忽略大小寫

huey@huey-K42JE:~/huey/linux/cmdline$ grep -i com test.txtSimple is better than complexComplex is better than complicated

c) 輸出以 "complex" 開頭的行,忽略大小寫

huey@huey-K42JE:~/huey/linux/cmdline$ grep -i '^complex' test.txtComplex is better than complicated

d) 輸出以 "idea" 結(jié)尾的行

huey@huey-K42JE:~/huey/linux/cmdline$ grep 'idea$' test.txtIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good idea

e) 匹配空行

huey@huey-K42JE:~/huey/linux/cmdline$ grep -n '^$' test.txt2:

h) 輸出含有 "good" 或 "bad" 的行

huey@huey-K42JE:~/huey/linux/cmdline$ grep 'good/|bad' test.txtIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good ideahuey@huey-K42JE:~/huey/linux/cmdline$ grep -E 'good|bad' test.txtIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good idea

i) 精確匹配單詞 it,像 implicit、purity、purity 等這樣的單詞中的 it 部分是不會被匹配的

huey@huey-K42JE:~/huey/linux/cmdline$ grep -w it test.txtThere should be one-- and preferably only one --obvious way to do itIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good idea

j) 輸出含有 "Python" 的行及其后 3 行

huey@huey-K42JE:~/huey/linux/cmdline$ grep -A 3 Python test.txtThe Zen of Python, by Tim PetersBeautiful is better than uglyExplicit is better than implicit

相關(guān)命令

egrep - 相當(dāng)于 grep -E

fgrep - 相當(dāng)于 grep -F

pgrep - 相當(dāng)于 grep -P


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 客服| 共和县| 信阳市| 社旗县| 峡江县| 田林县| 萨嘎县| 淳安县| 忻州市| 常宁市| 延庆县| 同德县| 灵武市| 赣榆县| 临泉县| 泰兴市| 拜城县| 葵青区| 江陵县| 葫芦岛市| 望奎县| 乌恰县| 龙江县| 吐鲁番市| 肇州县| 缙云县| 上杭县| 攀枝花市| 额尔古纳市| 德清县| 秦皇岛市| 东安县| 荥阳市| 越西县| 治县。| 巴马| 错那县| 金华市| 同德县| 即墨市| 道孚县|