首頁  >  文章  >  系統教程  >  Linux系統中grep指令的實戰案例分析及參考案例解析

Linux系統中grep指令的實戰案例分析及參考案例解析

WBOY
WBOY原創
2024-06-02 19:04:13589瀏覽

目錄一.grep指令介紹二.句型格式及常用選項三.參考案例3.1搜尋檔案中以root開頭的檔案3.2搜尋檔案中出現的root3.3搜尋不僅符合行之外的行3.4相符的部份使用顏色顯示3.5只輸出文件中匹配到的地方3.6輸出包含匹配字符串的行,並顯示所在的行數3.7統計文件或則文本中包含匹配字符串的行數總結

一.grep指令介紹Linux系統中grep指令是一種強悍的文本搜尋工具,它能使用正規表示式搜尋文本,並把符合的行複印下來。

grep全名為GlobalRegularExpressionPrint,表示全域正規表示式版本,它的使用權限是所有使用者。

中文註解:

grep['grep]搜尋目標行命令·global[?glo?bl]全球的,絲狀的regular美[?r?ɡj?l?]有規律的,規則的,正規軍(n)expression美[?k?spr???n]抒發,表現,表情,面色,心態

例句:It'senoughtomakeyouwetyourself,ifyou'llpardontheexpression

linux支援三種方式的grep指令:grep,egrep,grep-E

二.句型格式及常用選項依慣例,我們還是先查看幫助,使用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-regexpPATTERN 是一个基本正则表达式(缩写为 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 字节的数据行,但不是空行
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-matchselect non-matching lines
-V, --version display version information and exit
--helpdisplay this help text and exit
输出控制:
-m, --max-count=NUM NUM 次匹配后停止
-b, --byte-offset 输出的同时打印字节偏移
-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, --textequivalent to --binary-files=text
-Iequivalent to --binary-files=without-match
-d, --directories=ACTIONhow to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTIONhow 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-matchesprint 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, --nullprint 0 byte after FILE name
文件控制:
-B, --before-context=NUM打印以文本起始的NUM 行
-A, --after-context=NUM 打印以文本结尾的NUM 行
-C, --context=NUM 打印输出文本NUM 行
-NUMsame as --context=NUM
--group-separator=SEP use SEP as a group separator
--no-group-separatoruse empty string as a group separator
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binarydo not strip CR characters at EOL (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows)

為了更直覺一些,我們把常用的參數用表格來展示:

參數描述-i忽略大小寫-E啟用POSTIX擴充正則表達式-P啟用perl正則-o只輸出正則表達式的匹配的內容-w整字匹配-v取反,也就是不匹配的- 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搜尋檔案中出現的root搜某個詞組,我們直接在grep前面跟中單字名子即可:

案例一:搜尋/etc/passwd中的root使用者

linux cat grep 匹配_grep正则表达式匹配数字_catgrep

#

[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/passwda.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參數,例如取反

#案例一:統計檔案的行數且不包含空白行

空白行的表示方式:^$

[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 ~]# 

linux cat grep 匹配_grep正则表达式匹配数字_catgrep

#有時侯我們更改了配置文件,文件中包含大量的#,我們想除去#查看內容,就可以使用

[root@mufenggrow ~]# grep -v ^# passwd |wc -l
35

3.4匹配的部份使用颜色显示这儿可以使用--color=auto,我们来查看一下包含root的行linux cat grep 匹配linux cat grep 匹配,并高亮显示要查找的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 ~]# 

这样显示,疗效不显著,我们看右图:

catgrep_grep正则表达式匹配数字_linux cat grep 匹配

catgrep_linux cat grep 匹配_grep正则表达式匹配数字

以看见,所有的root都是蓝色表示的。

3.5只输出文件中匹配到的地方例如我们要查询root,但我不想显示包含root的行RAR FOR LINUX,而是只显示要查询的内容:

此时须要使用-o参数,代码如下

[root@mufenggrow ~]# grep -o root /etc/passwd
root
root
root
root

要注意,假若一行中有10个root,这儿就显示10个,而不是只显示一个,所以3.4的案例中我们查询的时侯红帽子linux下载,包含root的有两行,但有4个root,在3.5案例中,显示了所有的root。

3.6输出包含匹配字符串的行,并显示所在的行数此处可以使用-n参数,-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

我们要统计一个文件一共有多少行,也可以使用-n参数

root@mufenggrow ~]# grep -n "" passwd |awk -F : '{print $1}' |tail -n 1
35

3.7统计文件或则文本中包含匹配字符串的行数此时可以用-c参数:

[root@mufenggrow ~]# grep -c "root" passwd
2

包含root的有两行,假如我们要统计文本的行数:

[root@mufenggrow ~]# grep -c "$" passwd
35

相当于查找$的行数,可以见到一共有35个$符号,也就是35行。

总结grep命令在日常工作中,应用的比较广泛,一定要认真学习,记熟记牢常用参数。

到此这篇关于linux中grep命令使用实战解读的文章就介绍到这了,更多相关linuxgrep命令内容请搜索曾经的文章或继续浏览下边的相关文章希望你们之后多多支持!

以上是Linux系統中grep指令的實戰案例分析及參考案例解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn