linux怎么對文本或輸出內容進行過濾-創(chuàng)新互聯(lián)

這篇文章主要講解了“l(fā)inux怎么對文本或輸出內容進行過濾”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“l(fā)inux怎么對文本或輸出內容進行過濾”吧!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于成都網(wǎng)站設計、成都網(wǎng)站制作、南康網(wǎng)絡推廣、小程序開發(fā)、南康網(wǎng)絡營銷、南康企業(yè)策劃、南康品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供南康建站搭建服務,24小時服務熱線:18980820575,官方網(wǎng)址:muchs.cn

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

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

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

[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 /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

注意以上兩個例子中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/nologin
tcpdump:x:72:72::/:/sbin/nologin

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

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

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

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

[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統(tǒng)計匹配的行數(shù):

[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中的每一行作為模式匹配:

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

選項-x表示整行匹配:

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

選項-w表示匹配整個單詞:

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

選項-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

選項-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為它的字面意思匹配(忽略字符的特殊含義),等同于執(zhí)行命令fgrep:

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

命令無輸出

選項-E可以使用擴展的正則表達式,如同執(zhí)行egrep命令:

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

使用擴展正則表達式意味著不需要轉義就能表示字符的特殊含義,包括?,+,{,|,(和)。

選項-P表示使用perl的正則表達式進行匹配
如:

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

perl正則中"\d"表示數(shù)字,+表示匹配一到多次(同vim)。

選項-a將二進制文件當成文本文件處理:

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

選項--exclude=GLOB和--include=GLOB分別表示排除和包含匹配GLOB的文件,GLOB表示通配符(find及xargs用法見基礎命令介紹三):

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

grep強大的過濾能力來自于各種選項以及正則表達式的配合,在今后的文章中還有更多的例子。

感謝各位的閱讀,以上就是“l(fā)inux怎么對文本或輸出內容進行過濾”的內容了,經(jīng)過本文的學習后,相信大家對linux怎么對文本或輸出內容進行過濾這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!

網(wǎng)站欄目:linux怎么對文本或輸出內容進行過濾-創(chuàng)新互聯(lián)
文章URL:http://muchs.cn/article30/degepo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、品牌網(wǎng)站設計、外貿建站、全網(wǎng)營銷推廣、用戶體驗定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站