grep
即(global regular expression print)子眶;Linux系統(tǒng)中g(shù)rep命令是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本彪杉,并把匹配的行/特定內(nèi)容打印出來灭抑。
匹配特征:grep默認(rèn)一些簡單正則,如^,$等漾稀,
參數(shù)多模闲;常用的也多
例子
nl /etc/passwd | grep 'qmcui' # 最好帶引號(hào)
nl /etc/passwd | grep -v 'qmcui'
nl /etc/passwd | grep -w "root"
nl /etc/passwd | grep ROOT
nl /etc/passwd | grep -i ROOT
nl /etc/passwd | grep -ie 'Server' -e 'root' -e 'qmcui'
nl /etc/passwd | grep 'root\|qmcui'
nl /etc/passwd | grep -ie 'Server' -e 'root\|qmcui'
## 區(qū)別下面代碼
# nl /etc/passwd | grep 'root\|qmcui' # 轉(zhuǎn)義成正則
nl /etc/passwd | grep 'root' | grep 'qmcui'
echo $PATH | grep ':'
echo $PATH | grep -o ':'
echo $PATH | sed 's/:/\n/g' | grep -A 1 '/sbin'
echo $PATH | sed 's/:/\n/g' | grep '/sbin' -A 1
echo $PATH | sed 's/:/\n/g' | grep '/sbin' -B 1
echo $PATH | sed 's/:/\n/g' | grep '/sbin' -C 1
nl /etc/passwd | grep -c 'qmcui'
nl /etc/passwd | grep -ie 'Server' -e 'root\|qmcui' -c
nl /etc/passwd | grep -n 'qmcui'
# $ cat match.txt # cat >match.txt
# root
# qmcui
less /etc/passwd | grep -f match.txt
unalias grep
cat /etc/passwd | grep '^root'
cat /etc/passwd | grep --color=auto 'root'
nl /etc/passwd | grep --color=auto 'bash$'
# 進(jìn)階,不要求掌握崭捍!
nl /etc/passwd | grep -E 'root|qmcui'
grep '[^a]bb' 1.txt # 查bb前一個(gè)字符不為a的bb
grep 'a..b' 1.txt # 開頭為a 結(jié)尾為b 長度為4
cat /etc/passwd|grep --color=auto 'o\{2\}'
cat /etc/passwd|grep --color=auto 'o\{2,\}'
seq 20 | grep '[10]'
參數(shù)詳解
grep --help: 首先學(xué)會(huì)長參數(shù)尸折,短參數(shù)
-v
–invert-match:取沒有匹配的行
-w
–word-regexp:只選擇匹配上整個(gè) words
-i
,–ignore-case:忽略Pattern 和文件內(nèi)容的大小寫
-e
:多個(gè)-e 可以用來描述多個(gè)不同的匹配模式 殷蛇;or
-o
:-n形式輸出匹配內(nèi)容
-A
:輸出匹配行之后的num行
-B
:
-C
NUM–context=NUM 表示前后各NUM行
-c
輸出計(jì)數(shù)后的幾行
-n
–line-number 顯示行號(hào)
-f
FILE 实夹,–file=FILE 從FILE中獲得匹配的數(shù)據(jù)
--color=auto
很有用,你看的出來
#### 彩蛋:
find /public/ -name '*gz' 2>null
學(xué)習(xí)鏈接
參考:https://blog.csdn.net/successdd/article/details/79088926?>
https://blog.csdn.net/shenhuan1104/article/details/75852822
不好:https://blog.csdn.net/poison_biti/article/details/75581410