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

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

linux文本過濾grep基礎(chǔ)命令介紹(5)

2019-10-26 18:49:41
字體:
供稿:網(wǎng)友

在linux中經(jīng)常需要對文本或輸出內(nèi)容進(jìn)行過濾,最常用的過濾命令是grep

grep [OPTIONS] PATTERN [FILE...]
grep按行檢索輸入的每一行,如果輸入行包含模式PATTERN,則輸出這一行。這里的PATTERN是正則表達(dá)式(參考前一篇,本文將結(jié)合grep一同舉例)。

輸出文件/etc/passwd中包含root的行:

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

或者從標(biāo)準(zhǔn)輸入獲得:

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

需要注意的地方是:當(dāng)grep的輸入既來自文件也來自標(biāo)準(zhǔn)輸入時,grep將忽略標(biāo)準(zhǔn)輸入的內(nèi)容不做處理,除非使用符號-來代表標(biāo)準(zhǔn)輸入:

[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(標(biāo)準(zhǔn)輸入):root:x:0:0:root:/root:/bin/bash(標(biāo)準(zhǔn)輸入):operator:x:11:0:operator:/root:/sbin/nologin

此時,grep會標(biāo)明哪些結(jié)果來自于文件哪些來自于標(biāo)準(zhǔn)輸入。

輸出文件/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結(jié)尾的行:

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

注意以上兩個例子中PATTERN被雙引號引用起來以防止被shell解析。

輸出文件/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/nologintcpdump:x:72:72::/:/sbin/nologin

這里需要理解兩個^間不同的含義,第一個^表示行首,第二個在[]內(nèi)部的首個字符^表示取反。

輸出文件/etc/passwd中字符0連續(xù)出現(xiàn)3次及以上的行(注意轉(zhuǎn)義字符'/'):

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

如輸出文件/etc/passwd中以字符r或l開頭的行:

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

選項-i使grep在匹配模式時忽略大小寫:

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

選項-o表示只輸出匹配的字符,而不是整行:

[root@centos7 temp]# grep -oi abcd file ABCDabcd[root@centos7 temp]#            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 稻城县| 河池市| 衢州市| 中西区| 岐山县| 文安县| 商水县| 宝鸡市| 类乌齐县| 灵山县| 通渭县| 丰原市| 宁蒗| 任丘市| 嘉义市| 乌什县| 龙州县| 会昌县| 若羌县| 辽中县| 景德镇市| 达州市| 霍邱县| 广宁县| 丹东市| 密山市| 怀安县| 长海县| 荃湾区| 紫阳县| 科尔| 营山县| 南江县| 江川县| 奎屯市| 湘阴县| 拜城县| 噶尔县| 镇安县| 湘阴县| 永吉县|