linux中grep命令使用實戰(zhàn)詳解
一. grep命令介紹
Linux系統(tǒng)中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。
grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。
英文注解:
grep ['grep] 搜索目標行命令· global [?glo?bl] 全球的,球狀的 regular 美 [?r?ɡj?l?] 有規(guī)律的,規(guī)則的, 正規(guī)軍(n) expression 美 [?k?spr???n] 表達,表現(xiàn),表情,臉色,態(tài)度
例句: It's enough to make you wet yourself, if you'll pardon the expression
linux支持三種形式的grep命令: grep , egrep ,grep -E
二. 語法格式及常用選項
依據(jù)慣例,我們還是先查看幫助,使用grep --help
[root@mufeng test]# grep --help
用法: grep [選項]... PATTERN [FILE]...
在每個 FILE 或是標準輸入中查找 PATTERN。
默認的 PATTERN 是一個基本正則表達式(縮寫為 BRE)。
例如: grep -i 'hello world' menu.h main.c
正則表達式選擇與解釋:
-E, --extended-regexp PATTERN 是一個可擴展的正則表達式(縮寫為 ERE)
-F, --fixed-strings PATTERN 是一組由斷行符分隔的定長字符串。
-G, --basic-regexp PATTERN 是一個基本正則表達式(縮寫為 BRE)
-P, --perl-regexp PATTERN 是一個 Perl 正則表達式
-e, --regexp=PATTERN 用 PATTERN 來進行匹配操作
-f, --file=FILE 從 FILE 中取得 PATTERN
-i, --ignore-case 忽略大小寫
-w, --word-regexp 強制 PATTERN 僅完全匹配字詞
-x, --line-regexp 強制 PATTERN 僅完全匹配一行
-z, --null-data 一個 0 字節(jié)的數(shù)據(jù)行,但不是空行
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
輸出控制:
-m, --max-count=NUM NUM 次匹配后停止
-b, --byte-offset 輸出的同時打印字節(jié)偏移
-n, --line-number 輸出的同時打印行號
--line-buffered 每行輸出清空
-H, --with-filename 為每一匹配項打印文件名
-h, --no-filename 輸出時不顯示文件名前綴
--label=LABEL 將LABEL 作為標準輸入文件名前綴
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive
likewise, but follow all symlinks
--include=FILE_PATTERN
search only files that match FILE_PATTERN
--exclude=FILE_PATTERN
skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
-l, --files-with-matches print only names of FILEs containing matches
-c, --count print only a count of matching lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
文件控制:
-B, --before-context=NUM 打印以文本起始的NUM 行
-A, --after-context=NUM 打印以文本結(jié)尾的NUM 行
-C, --context=NUM 打印輸出文本NUM 行
-NUM same as --context=NUM
--group-separator=SEP use SEP as a group separator
--no-group-separator use empty string as a group separator
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows)
為了更直觀一些,我們把常用的參數(shù)用表格來展示:
| 參數(shù) | 描述 |
|---|---|
| -i | 忽略大小寫 |
| -E | 啟用POSTIX擴展正則表達式 |
| -P | 啟用perl正則 |
| -o | 只輸出正則表達式的匹配的內(nèi)容 |
| -w | 整字匹配 |
| -v | 取反,也就是不匹配的 |
| -n | 輸出行號 |
有了具體的參數(shù)之后,我們再來看實戰(zhàn)案例:
三. 參考案例
3.1 搜索文件中以root開頭的文件
以root開頭的文件,可以用 ^root 比如查看/etc/passwd 中以root開頭的文件,操作如下:
[root@mufenggrow ~]# grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash
3.2 搜索文件中出現(xiàn)的root
搜某個單詞,我們直接在grep后面跟上單詞名字即可:
案例一: 搜索/etc/passwd中的root用戶
[root@mufenggrow ~]# grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@mufenggrow ~]#
案例二: 從多個文件中搜索root
root@mufenggrow ~]# echo root >> a.txt [root@mufenggrow ~]# echo root >> b.txt [root@mufenggrow ~]# grep "root" /etc/passwd a.txt b.txt /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin a.txt:root b.txt:root [root@mufenggrow ~]#
3.3 搜索除了匹配行之外的行
此處使用-v 參數(shù),比如取反
案例一: 統(tǒng)計文件的行數(shù)且不包含空行
空行的表示方法: ^$
[root@mufenggrow ~]# cp /etc/passwd ./ ## 源文件一共35行 [root@mufenggrow ~]# cat /etc/passwd |wc -l 35 ## 追加空行進去 [root@mufenggrow ~]# echo "" >> /etc/passwd [root@mufenggrow ~]# cat /etc/passwd |wc -l 36 ## 去掉空行測試 [root@mufenggrow ~]# grep -v ^$ /etc/passwd |wc -l 35 [root@mufenggrow ~]#
有時候我們修改了配置文件,文件中包含大量的# ,我們想去掉#查看內(nèi)容,就可以使用
[root@mufenggrow ~]# grep -v ^# passwd |wc -l 35
3.4 匹配的部分使用顏色顯示
這里可以使用 --color=auto,我們來查看一下包含root的行,并高亮顯示要查找的root。
[root@mufenggrow ~]# grep root /etc/passwd --color=auto root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@mufenggrow ~]#
這樣顯示,效果不明顯,我們看下圖:
可
以看到,所有的root都是紅色表示的。
3.5 只輸出文件中匹配到的地方
比如我們要查詢root,但我不想顯示包含root的行,而是只顯示要查詢的內(nèi)容:
此時需要使用 -o 參數(shù),代碼如下
[root@mufenggrow ~]# grep -o root /etc/passwd root root root root
要注意,如果一行中有10個root,這里就顯示10個,而不是只顯示一個,所以3.4的案例中我們查詢的時候,包含root的有兩行,但有4個root,在3.5案例中,顯示了所有的root。
3.6 輸出包含匹配字符串的行,并顯示所在的行數(shù)
此處可以使用-n 參數(shù), -n 會在一行的前面加上 行號: 比如“4:”
我們來看下代碼示例:
[root@mufenggrow ~]# grep -n "root" passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin
我們要統(tǒng)計一個文件一共有多少行,也可以使用-n 參數(shù)
root@mufenggrow ~]# grep -n "" passwd |awk -F : '{print $1}' |tail -n 1
35
3.7 統(tǒng)計文件或者文本中包含匹配字符串的行數(shù)
此時可以用-c參數(shù):
[root@mufenggrow ~]# grep -c "root" passwd 2
包含root的有兩行, 如果我們要統(tǒng)計文本的行數(shù):
[root@mufenggrow ~]# grep -c "$" passwd 35
相當于查找 $的行數(shù),可以看到一共有35個$符號,也就是35行。
總結(jié)
grep命令在日常工作中,應用的比較廣泛,一定要認真學習,記熟記牢常用參數(shù)。
到此這篇關于linux中grep命令使用實戰(zhàn)詳解的文章就介紹到這了,更多相關linux grep命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
從Windows到Linux的三種文件傳輸實現(xiàn)方式
文章介紹了在跨平臺工作環(huán)境中將文件從Windows系統(tǒng)傳輸?shù)絃inux服務器的幾種方法,包括使用SCP命令、搭建Web服務和使用wget下載文件,每種方法都有其特點,如安全性、速度、易用性和適用場景等2024-12-12
Apache Flink 任意 JAR 包上傳漏洞利用及防范策略分析
這篇文章主要介紹了Apache Flink 任意 JAR 包上傳漏洞利用及防范策略, 本文提供的案例和方法僅用于教學目的,實際操作時應確保遵守相關法律法規(guī),不應用于非法侵入或攻擊他人系統(tǒng),需要的朋友可以參考下2024-07-07
詳解CentOS 6.4 添加永久靜態(tài)路由所有方法匯總
這篇文章主要介紹了詳解CentOS 6.4 添加永久靜態(tài)路由所有方法匯總,非常具有實用價值,需要的朋友可以參考下。2016-12-12
LNMP下添加Discuz x2.5的偽靜態(tài)規(guī)則
今天一個找我配置過 lnmp 環(huán)境的用戶要搭建 Discuz 論壇,但是發(fā)現(xiàn)不支持偽靜態(tài),倡萌沒怎么折騰過 Discuz,百度下才知道,原來軍哥的 LNMP 環(huán)境包自帶的 DZ 規(guī)則不適用于 Discuz x2.5,記錄下對應的偽靜態(tài)規(guī)則2016-09-09

