Linux(Centos 7.6)命令详解:grep

1.命令作用

从文本文件或标准输出中通过管道筛选匹配的行及数据(Search for PATTERN in each FILE or standard input.);默认遵循基本的正则表达式,也可指定-E遵循扩展正则表达式。

2.命令语法

Usage: grep [OPTION]... PATTERN [FILE]...

3.参数详解

OPTION:

Regexp selection and interpretation(正则表达式选择与解释):

  • -E, --extended-regexp,将 PATTERN 内容视为扩展正则表达式(ERE),其中的 '|'、'*'、'.'、'^' 会被正则匹配
  • -F, --fixed-strings,将 PATTERN 内容视为一组以换行符分隔的固定字符串,其中的 '|'、'*'、'.'、'^' 不会正则匹配,认为是普通的字符串内容
  • -G, --basic-regexp,将 PATTERN 视为基本正则表达式(BRE),BRE支持的基础正则元字符(如'.'、'*'、'$');如需使用扩展正则(如'+'、'?'、'|'),需要转义(如'\+'、'\?'、'\|')才能生效;若未指定-E、-F、P,grep默认启动该参数
  • -P, --perl-regexp,将 PATTERN 内容视为 Perl 正则表达式
  • -e, --regexp=PATTERN,可使用单个或多个-e,其指定的 PATTERN 以或方式进行匹配
  • -f, --file=FILE,从指定文件中获取 PATTERN,文件中可以指定多个PATTERN进行多个匹配,默认遵循BRE,添加-E则遵循ERE
  • -i, --ignore-case,忽略大小写差异
  • -w, --word-regexp,强制 PATTERN 仅匹配完整单词
  • -x, --line-regexp,强制 PATTERN 仅匹配整行
  • -z, --null-data,数据行以 0 字节结尾,而非换行符

Miscellaneous(其他选项):

  • -s, --no-messages,抑制错误信息
  • -v, --invert-match,选择不匹配的行
  • -V, --version,显示版本信息并退出
  • --help,显示帮助信息

Output control(输出控制):

  • -m, --max-count=NUM,NUM次匹配后停止
  • -b, --byte-offset,输出行时打印字节偏移量
  • -n, --line-number,输出行时打印行号
  • --line-buffered,每行上刷新输出;默认块缓冲输出,指定行缓冲输出
  • -H, --with-filename,打印时输出匹配行的文件名;单个文件搜索默认不显示文件名,多个文件搜索默认显示文件名
  • -h, --no-filename,打印时不输出匹配行的文件名;单个文件搜索默认不显示文件名,多个文件搜索默认显示文件名;与-H相反
  • --label=LABEL,使用LABEL作为标准输入文件名前缀
  • -o, --only-matching,只显示匹配PATTERN行内容的PATTERN部分
  • -q, --quiet, --silent,抑制所有正常输出
  • --binary-files=TYPE,假设二进制文件是TYPE ;TYPE可为'binary','text',or 'without-match'
  • -a, --text,相当于--binary-files=text
  • -I,相当于--binary-files=without-match
  • -d, --directories=ACTION,如何处理目录;ACTION可为'read', 'recurse', or 'skip'
  • -D, --devices=ACTION,如何处理设备(devices)、FIFOs、sockets;ACTION可为'read','skip'
  • -r, --recursive,相当于--directories=recurse
  • -R, --dereference-recursive              likewise, but follow all symlinks
  • --include=FILE_PATTERN,仅搜索FILE_PATTERN匹配的文件内容
  • --exclude=FILE_PATTERN,跳过FILE_PATTERN匹配的文件和目录
  • --exclude-from=FILE,从file中跳过匹配任何文件模式的文件
  • --exclude-dir=PATTERN,匹配PATTERN的目录将被跳过。
  • -L, --files-without-match,只打印不包含匹配项的文件名
  • -l, --files-with-matches,只打印包含匹配项的文件名
  • -c, --count,只打印每个文件的匹配行数
  • -T, --initial-tab,使选项卡对齐(如果需要)
  • -Z, --null,在文件名后打印0字节

Context control:

  • -B, --before-context=NUM,打印前导上下文的NUM行
  • -A, --after-context=NUM,打印尾上下文的NUM行
  • -C, --context=NUM,输出上下文的NUM行
  • -NUM,相当于--context=NUM
  • --group-separator=SEP,使用SEP作为组分隔符
  • --no-group-separator,使用空字符串作为组分隔符
  • --color[=WHEN], --colour[=WHEN],使用标记突出显示匹配的字符串;WHEN可为强制高亮('always'), 禁止高亮('never'), or 自动('auto')输出终端时高亮
  • -U, --binary,直接以二进制模式搜索,不进行换行符转换(do not strip CR characters at EOL) (MSDOS/Windows)
  • -u, --unix-byte-offsets,强制按LF换行符计算偏移量(忽略CRLF中的\r字符)report offsets as if CRs were not there (MSDOS/Windows)

4.常用用例

4.1.正则表达式选择与解释

4.1.1.几种正则匹配(默认基本正则、扩展正则、不以正则匹配)

[root@node1 ~]# cat test.txt 					# 文件内容
Never give up,
Never lose hope.
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
So put on a smile,
You'll live through your pain.
Know it will pass,
And strength you will gain.
开心果果
192.168.11.22
start|end
nostart
^kaishishi
[root@node1 ~]# grep -G '^S' test.txt             # -G基本正则,^S是以S开头的行
So put on a smile,
[root@node1 ~]# grep '^S' test.txt                # 默认基本正则(-G可省略),^S是以S开头的行
So put on a smile,
[root@node1 ~]# 
[root@node1 ~]# grep 'Never|dreams' test.txt      # 默认基本正则,|不会被视为正则符号
[root@node1 ~]# grep -E 'Never|dreams' test.txt   # -E会启动扩展正则匹配,会将|视为或者
Never give up,
Never lose hope.
Your dreams will come true.
[root@node1 ~]# 
[root@node1 ~]# grep -F '^k' test.txt             # 取消正则匹配,^被认为是普通字符
^kaishishi
[root@node1 ~]# grep -P '\p{Han}' test.txt        # perl正则匹配,输出汉字的行
开心果果
[root@node1 ~]# 

4.1.2.多个PATTERN匹配

[root@node1 ~]# grep -e 'Always' test.txt 
Always have faith,
[root@node1 ~]# grep -e 'Trying' test.txt 
Trying times will pass,
[root@node1 ~]# grep -e 'Always' -e 'Trying' test.txt 
Always have faith,
Trying times will pass,
[root@node1 ~]#

4.1.3.忽略大小写匹配

[root@node1 ~]# grep 'trying' test.txt 
[root@node1 ~]# grep -i 'Trying' test.txt 
Trying times will pass,
[root@node1 ~]# 

4.1.4.完整单词匹配

[root@node1 ~]# grep -w 'Trying' test.txt 
Trying times will pass,
[root@node1 ~]# grep -w 'Tryin' test.txt 
[root@node1 ~]#

4.1.5.整行匹配

[root@node1 ~]# grep -x 'Never give up' test.txt 
[root@node1 ~]# grep -x 'Never give up,' test.txt 
Never give up,
[root@node1 ~]# 

4.2.其他选项

4.2.1.抑制错误信息

[root@node1 ~]# grep "error" /opt/error.log
grep: /opt/error.log: No such file or directory
[root@node1 ~]# grep -s "error" /opt/error.log
[root@node1 ~]# 

4.2.2.选择不匹配的行

[root@node1 ~]# echo -e "Never\nTrying"
Never
Trying
[root@node1 ~]# echo -e "Never\nTrying" | grep 'Never'
Never
[root@node1 ~]# echo -e "Never\nTrying" | grep -v 'Never'
Trying
[root@node1 ~]# 

4.3.输出控制

4.3.1.匹配几次后退出

[root@node1 ~]# grep 'y' test.txt
Always have faith,
It allows you to cope.
Trying times will pass,
As they always do.
You'll live through your pain.
And strength you will gain.
[root@node1 ~]# grep -m 3 'y' test.txt
Always have faith,
It allows you to cope.
Trying times will pass,
[root@node1 ~]#

4.3.2.输出时打印字节偏移量

# 第一行显示32,是当前行之前内容的字节数,除每个字母占一个字节,每个中文占3个字节,换行占一个字节
[root@node1 ~]# grep -b 'y' test.txt
32:Always have faith,
51:It allows you to cope.
74:Trying times will pass,
98:As they always do.
184:You'll live through your pain.
234:And strength you will gain.
[root@node1 ~]#

4.3.3.输出时打印行号

[root@node1 ~]# grep -n 'y' test.txt
3:Always have faith,
4:It allows you to cope.
5:Trying times will pass,
6:As they always do.
10:You'll live through your pain.
12:And strength you will gain.
[root@node1 ~]# 

4.3.4.仅显示匹配行中的匹配内容

[root@node1 ~]# grep 'Never' test.txt
Never give up,
Never lose hope.
[root@node1 ~]# grep -o 'Never' test.txt
Never
Never
[root@node1 ~]#

4.3.5.静默输出

[root@node1 ~]# grep 'Never' test.txt
Never give up,
Never lose hope.
[root@node1 ~]# grep -q 'Never' test.txt  # -q静默输出,但是有输出的,$?值为真(0)
[root@node1 ~]# echo $?
0
[root@node1 ~]# 
[root@node1 ~]# grep 'jingjing' test.txt  # 没有匹配输出,$?值为假(非0)
[root@node1 ~]# echo $?
1
[root@node1 ~]# 

4.3.6.二进制文件搜索方式

[root@node1 ~]# grep --binary-files=text "keyword" /usr/bin/*           # 二进制文件以text文件进行搜索,会有一些乱码输出
[root@node1 ~]# grep --binary-files=binary "keyword" /usr/bin/*         # 二进制文件以binary文件进行搜索,输出不会出现乱码
[root@node1 ~]# grep --binary-files=without-match "keyword" /usr/bin/*  # 二进制文件以without-match文件进行搜索,是binary的基础上,不输出没有匹配内容的二进制文件名称

4.3.7.关于目录的搜索方式

[root@node1 ~]# grep "error" /var/log/             # 默认视目录为普通文件
grep: /var/log/: Is a directory
[root@node1 ~]# grep -d read "error" /var/log/     # 视目录为普通文件,可忽略参数
grep: /var/log/: Is a directory
[root@node1 ~]# grep -d recurse "error" /var/log/  # 递归目录搜索
... ...
[root@node1 ~]# grep -d skip "error" /var/log/*    # 仅搜索/var/log目录下的文件,跳过目录下所有的子目录

4.3.8.搜索指定文件或者排除指定文件,可用*/?通配符匹配

[root@node1 ~]# grep --include=*.txt 'Trying' Desktop/*
grep: Desktop/dir1: Is a directory
grep: Desktop/dir2: Is a directory
grep: Desktop/dir3: Is a directory
grep: Desktop/root: Is a directory
Desktop/test2.txt:Trying
Desktop/test.txt:Trying times will pass,
[root@node1 ~]# 
[root@node1 ~]# grep --exclude=*2.txt 'Trying' Desktop/*
grep: Desktop/dir1: Is a directory
grep: Desktop/dir2: Is a directory
grep: Desktop/dir3: Is a directory
grep: Desktop/root: Is a directory
Desktop/test.txt:Trying times will pass,
[root@node1 ~]# 
[root@node1 ~]# grep --exclude="*.txt" 'Trying' Desktop/*
grep: Desktop/dir1: Is a directory
grep: Desktop/dir2: Is a directory
grep: Desktop/dir3: Is a directory
grep: Desktop/root: Is a directory
[root@node1 ~]# cat aaa 
*.txt
[root@node1 ~]# grep --exclude-from=aaa 'Trying' Desktop/*
grep: Desktop/dir1: Is a directory
grep: Desktop/dir2: Is a directory
grep: Desktop/dir3: Is a directory
grep: Desktop/root: Is a directory
[root@node1 ~]#  
[root@node1 ~]# grep -r 'jingjingjing' Desktop/*
Desktop/file2.txt:jingjingjing
Desktop/file5.txt:jingjingjing
Desktop/root/Desktop/dir1/file5.txt:jingjingjing
[root@node1 ~]# grep -r --exclude-dir=root 'jingjingjing' Desktop/*
Desktop/file2.txt:jingjingjing
Desktop/file5.txt:jingjingjing
[root@node1 ~]# 

4.3.9.打印匹配/不匹配的文件名称

[root@node1 dir1]# grep 'jingjingjing' *
file5.txt:jingjingjing
Binary file file.tar matches
[root@node1 dir1]# grep -l 'jingjingjing' *
file5.txt
file.tar
[root@node1 dir1]# grep -L 'jingjingjing' *
file6.txt
file7.txt
file8.txt
file9.txt
filelist
[root@node1 dir1]#

4.3.10.打印匹配文件及其匹配的行数

[root@node1 ~]# grep 'Never' test*.txt
test.txt:Never give up,
test.txt:Never lose hope.
[root@node1 ~]# grep -c 'Never' test*.txt
test1.txt:0
test2.txt:0
test.txt:2
[root@node1 ~]# 

4.4.Context control

4.4.1.打印匹配行行及其前n/后n行/前后n行

[root@node1 ~]# grep 'Trying' test.txt 
Trying times will pass,
[root@node1 ~]# grep -B 2 'Trying' test.txt 
Always have faith,
It allows you to cope.
Trying times will pass,
[root@node1 ~]# grep -A 3 'Trying' test.txt 
Trying times will pass,
As they always do.
Just have patience,
Your dreams will come true.
[root@node1 ~]# 
[root@node1 ~]# grep -C 1 'Trying' test.txt 
It allows you to cope.
Trying times will pass,
As they always do.
[root@node1 ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆是浪个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值