日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

_linux文本过滤grep基础命令介绍

發(fā)布時(shí)間:2025/3/15 linux 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 _linux文本过滤grep基础命令介绍 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在linux中經(jīng)常需要對(duì)文本或輸出內(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)輸入時(shí),grep將忽略標(biāo)準(zhǔn)輸入的內(nèi)容不做處理,除非使用符號(hào)-來代表標(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

此時(shí),grep會(huì)標(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

注意以上兩個(gè)例子中PATTERN被雙引號(hào)引用起來以防止被shell解析。

輸出文件/etc/passwd中不以a-s中任何一個(gè)字母開頭的行:

[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

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

輸出文件/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

選項(xiàng)-i使grep在匹配模式時(shí)忽略大小寫:

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

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

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

選項(xiàng)-c統(tǒng)計(jì)匹配的行數(shù):

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

選項(xiàng)-v表示取反匹配,如輸出/etc/passwd中不以/sbin/nologin結(jié)尾的行:

[root@centos7 temp]# grep -v "/sbin/nologin$" /etc/passwdroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltlearner:x:1000:1000::/home/learner:/bin/bash

選項(xiàng)-f FILE表示以文件FILE中的每一行作為模式匹配:

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

選項(xiàng)-x表示整行匹配:

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

選項(xiàng)-w表示匹配整個(gè)單詞:

[root@centos7 temp]# grep here fileherethere[root@centos7 temp]# grep -w here filehere[root@centos7 temp]#

選項(xiàng)-h表示當(dāng)多個(gè)文件時(shí)不輸出文件名:

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

選項(xiàng)-n表示顯示行號(hào):

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

選項(xiàng)-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/passwdoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologin[root@centos7 temp]# grep -B2 ^operator /etc/passwd halt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin[root@centos7 temp]# grep -C1 ^operator /etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin

選項(xiàng)-F視PATTERN為它的字面意思匹配(忽略字符的特殊含義),等同于執(zhí)行命令fgrep:

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

命令無輸出

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

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

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

選項(xiàng)-P表示使用perl的正則表達(dá)式進(jìn)行匹配

如:

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

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

選項(xiàng)-a將二進(jìn)制文件當(dāng)成文本文件處理:

[root@centos7 ~]# grep -a online /usr/bin/ls%s online help: [root@centos7 ~]#選項(xiàng)--exclude=GLOB和--include=GLOB分別表示排除和包含匹配GLOB的文件,GLOB表示通配符(find及xargs用法見基礎(chǔ)命令介紹三):[root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash./test.sh:#!/bin/bash[root@centos7 temp]#

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的_linux文本过滤grep基础命令介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。