linux基础——grep

提示:grep (global search regular expression(RE) and print out the line,全⾯搜索正则表达式并把⾏打印出来)是⼀种强⼤的⽂本搜索⼯具,它能使⽤正则表达式搜索⽂本,并把匹配的⾏打印出来;
Unix的grep家族包括grepegrepfgrep。egrep和fgrep的命令只跟grep有很⼩不同。egrep是grep的扩展,⽀持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表⽰回其⾃⾝的字⾯意义,不再特殊。linux使⽤GNU版本的grep。它功能更强,可以通过-G、-E、-F命令⾏选项来使⽤egrep和fgrep的功能

一、grep是什么?

grep命令是global regular expression print(全局正则表达式输出)的缩写,它是linux系统中最强大的命令之一。它在一个或者多个文件中搜索与给定的内容的匹配的行,并将匹配的内容输出

二、grep语法格式

1.grep [参数]

- grep   [参数]   [目标过滤关键字]    [文件名称]
- cat/head/tail  |  grep  [参数]  [目标过滤关键字]
# 例:
# grep -i ads_prd resource.log
# cat resource.log | grep -i ads_prd

常用参数:

参数解释
-i忽略大小写
-v反转匹配,选择没有被匹配到的内容,返回"不" 包含指定关键字的结果
-n展示行号
-w精确匹配“单词整词”,不是“包含”的模糊like查询,而是精确查询,单词的两边必须是非字符符号(即不能是字母数字或下划线)
-A“after”,展示过滤的关键词所在行,及其“后”几行
-B“before”,展示过滤的关键词所在行,及其“前”几行
-C“after”+“before”,展示过滤的关键词所在行,及其前后各x行
-o逐行输出匹配的内容,有多少个匹配就有多少行,常和wc -l共用
–color高亮展示查询的关键词
-c只输出匹配行的数量
-r递归查询,匹配当前目录所有文件,以及子目录下的所有文件
\|以"或"的关系筛选过滤
-E以指定的"关键字"大小写来匹配过滤
-f输出后面文件中包含前面文件中内容的行
-l显示匹配的文件名

2.grep参数使用说明_示例

2.1 测试文件准备

在这里插入图片描述

2.2 参数 -i 忽略大小写

# 忽略大小写
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep test test01.txt 
test01
test 01

[root@localhost grep_test]# grep -i  test test01.txt 
test01
Test01
test 01
[root@localhost grep_test]# 

2.3 参数 -v 反向匹配

# 反转匹配,选择没有被匹配到的内容,返回"不" 包含指定关键字的结果
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep test test01.txt 
test01
test 01

[root@localhost grep_test]# grep -i test test01.txt 
test01
Test01
test 01

[root@localhost grep_test]# grep -v test test01.txt 
Test01
linux linux

[root@localhost grep_test]# grep -iv test test01.txt 
linux linux
[root@localhost grep_test]#

2.4 参数 -n 展示行号

# 展示行号
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep linux test01.txt 
linux linux

[root@localhost grep_test]# grep test test01.txt 
test01
test 01

[root@localhost grep_test]# grep -n linux test01.txt 
4:linux linux

[root@localhost grep_test]# grep -n test test01.txt 
1:test01
3:test 01

2.5 参数 -w 整词匹配

# 精确匹配“单词”,不是“包含”的模糊like查询,而是精确查询
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep test test01.txt 
test01
test 01

[root@localhost grep_test]# grep -w test test01.txt 
test 01

[root@localhost grep_test]# grep  lin test01.txt 
linux linux

[root@localhost grep_test]# grep  -w lin test01.txt 

2.6 参数 -A 展示关键词及向下n行

“after”,展示过滤的关键词所在行,及其“后”几行
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep test01 test01.txt 
test01

[root@localhost grep_test]# grep -A2 test01 test01.txt 
test01
Test01
test 01

[root@localhost grep_test]# grep -A3 test01 test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep test test01.txt 
test01
test 01

[root@localhost grep_test]# grep -A1 test test01.txt 
test01
Test01
test 01
linux linux

2.7 参数 -B 展示关键词及向上n行

“before”,展示过滤的关键词所在行,及其“前”几行
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep Test01 test01.txt 
Test01

[root@localhost grep_test]# grep -B1 Test01 test01.txt 
test01
Test01

[root@localhost grep_test]# grep -B2 Test01 test01.txt 
test01
Test01

2.8 参数 -C 展示关键词及上下各n行

“after”+“before”,展示过滤的关键词所在行,及其前后各x行
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep Tes test01.txt 
Test01

[root@localhost grep_test]# grep -C2 Tes test01.txt 
test01
Test01
test 01
linux linux

2.9 参数 -o 只显示匹配的关键字

逐行输出匹配的内容,有多少个匹配就有多少行,常和wc -l共用
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep -o e test01.txt 
e
e
e

[root@localhost grep_test]# grep -o te test01.txt 
te
te

[root@localhost grep_test]# grep -on te test01.txt 
1:te
3:te

[root@localhost grep_test]# grep -on te test01.txt  | wc -l
2

2.10 参数 --color 高亮展示过滤的关键字

# 高亮展示查询的关键词
[root@localhost grep_test]# cat test01.txt 
[root@localhost grep_test]# cat test01.txt 
[root@localhost grep_test]# grep --color test test01.txt 

在这里插入图片描述

2.11 参数 -c 只输出匹配行的数量

# 只输出匹配行的数量,-o 和 wc -l一起作用的功能相近
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep test test01.txt 
test01
test 01

[root@localhost grep_test]# grep -c test test01.txt 
2

2.12 参数 -r 递归查询

# 递归查询,匹配当前目录所有文件,以及子目录下的所有文件
[root@localhost grep_test]# ll
[root@localhost grep_test]# grep linux ./*
[root@localhost grep_test]# grep -r linux ./*

在这里插入图片描述

2.13 参数 \| 以或的关系过滤关键词

# 以"或"的关系筛选过滤
[root@localhost grep_test]# cat test01.txt 
[root@localhost grep_test]# grep 'test\|lin' test01.txt 

在这里插入图片描述

2.14 参数 -E 以指定的"关键字"大小写来匹配过滤

# 以指定的"关键字"大小写来匹配过滤
[root@localhost grep_test]# cat test01.txt 
[root@localhost grep_test]# grep test test01.txt 
[root@localhost grep_test]# grep -E test test01.txt 
[root@localhost grep_test]# grep -E T test01.txt 
[root@localhost grep_test]# grep -E l test01.txt 
[root@localhost grep_test]# grep -E lINUX test01.txt

在这里插入图片描述

2.15 参数 -f 输出后面文件中包含前面文件中内容的行

# 输出后面文件中包含前面文件中内容的行
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# cat test02.txt 
test02
Test02
test 02
linux linux
linux linuxaaabbb
linux aaabbb linux

[root@localhost grep_test]# grep -f test01.txt test02.txt 
linux linux
linux linuxaaabbb

[root@localhost grep_test]# grep -wf test01.txt test02.txt 
linux linux

2.16 参数 -l 显示匹配的文件名

[root@localhost grep_test]# ll
total 8
-rw-r--r--. 1 root root 34 May  8 00:36 test01.txt
-rw-r--r--. 1 root root 71 May  8 01:15 test02.txt
drwxr-xr-x. 2 root root 24 May  8 00:36 test0507

[root@localhost grep_test]# grep linux ./*.txt
./test01.txt:linux linux
./test02.txt:linux linux
./test02.txt:linux linuxaaabbb
./test02.txt:linux aaabbb linux

[root@localhost grep_test]# grep -l linux ./*.txt
./test01.txt
./test02.txt
[root@localhost grep_test]# grep -l linux *
test01.txt
test02.txt
grep: test0507: Is a directory

[root@localhost grep_test]# grep -lr linux *
test01.txt
test02.txt
test0507/test03.txt

2.17 通常grep解决行内匹配问题,跨行建议awk/sed/perl

3、grep匹配正则表达式

3.1 正则 ^ :匹配字符串“开始”位置

# ^ 为匹配输入字符串的开始位置【匹配以^后的字符“开始”的字符串】
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep "^t" *
test01.txt:test01
test01.txt:test 01
test02.txt:test02
test02.txt:test 02
grep: test0507: Is a directory

[root@localhost grep_test]# grep "^l" *
test01.txt:linux linux
test02.txt:linux linux
test02.txt:linux linuxaaabbb
test02.txt:linux aaabbb linux
grep: test0507: Is a directory

[root@localhost grep_test]# grep "^l" -r *
test01.txt:linux linux
test02.txt:linux linux
test02.txt:linux linuxaaabbb
test02.txt:linux aaabbb linux
test0507/test03.txt:linux linux

3.2 正则 $ :匹配字符串“结束”位置

[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep "1$" test01.txt 
test01
Test01
test 01

[root@localhost grep_test]# grep "01$" test01.txt 
test01
Test01
test 01
[root@localhost grep_test]# 

3.3 正则 < :匹配以指定"字符串"开始的行

[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep "<T" test01.txt 
[root@localhost grep_test]# grep "\<T" test01.txt 
Test01

[root@localhost grep_test]# grep "\<t" test01.txt 
test01
test 01

[root@localhost grep_test]# grep "\<e" test01.txt 
[root@localhost grep_test]# grep "\<nux" test01.txt

在这里插入图片描述

3.4 正则 > :匹配以指定"字符串"结束的行

[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep "\>x" test01.txt 
[root@localhost grep_test]# grep "x\>" test01.txt 
linux linux

3.5 [ ] : 单个字符,如[A] 即A 符合要求

[root@localhost grep_test]# grep [e] test01.txt
[root@localhost grep_test]# grep [st] test01.txt
[root@localhost grep_test]# grep st test01.txt

在这里插入图片描述

3.6 [^ ] : 显示"不包括"括号中字符串的所有行

[root@localhost grep_test]# cat test01.txt 
[root@localhost grep_test]# grep ^T test01.txt 
[root@localhost grep_test]# grep [^T] test01.txt 
[root@localhost grep_test]# grep [^k] test01.txt 
[root@localhost grep_test]# grep [^Test] test01.txt 
[root@localhost grep_test]# grep [^Test01] test01.txt 
[root@localhost grep_test]# grep [^Tes01] test01.txt

下图中从第三个命令开始,红色字体的都是不在[xxx]中的;
在这里插入图片描述

3.7 [ - ] : 范围匹配

# [-]范围匹配,如[A-C],即A、B、C都符合要求;如[a,b],即只有a和b符合要求
[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

[root@localhost grep_test]# grep [h-k] test01.txt 
linux linux

3.8 \ :用来屏蔽一个元字符的特殊含义,使其作为单纯的一个字符

[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux
^tabc
# 已t开头的
[root@localhost grep_test]# grep "^t" test01.txt 
test01
test 01

[root@localhost grep_test]# grep "\^t" test01.txt 
^tabc

三、简单使用

1、搜索非空行 -v “^$”

[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

^tabc

aab
[root@localhost grep_test]# grep "^$" test01.txt 


[root@localhost grep_test]# grep -v "^$" test01.txt 
test01
Test01
test 01
linux linux
^tabc
aab
[root@localhost grep_test]# grep -vn "^$" test01.txt 
1:test01
2:Test01
3:test 01
4:linux linux
6:^tabc
8:aab
[root@localhost grep_test]# 

2、搜索开头不是英文字母的行 ^ [^a-z,A-Z]

[root@localhost grep_test]# grep ^"[^a-z,A-Z]" test01.txt 
^tabc
[root@localhost grep_test]# grep "^[^a-z,A-Z]" test01.txt 
^tabc
[root@localhost grep_test]#

3、过滤以xx开头,yy结尾的行(.*起到了作用)

[root@localhost grep_test]# cat test01.txt 
test01
Test01
test 01
linux linux

^tabc

aab

[root@localhost grep_test]# grep "t.*1" test01.txt 
test01
Test01
test 01
[root@localhost grep_test]# grep "^t.*1" test01.txt 
test01
test 01
[root@localhost grep_test]#

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值