Linux grep 命令


linux中最为常用的三大文本(grep,sed,awk)处理工具

Linux sed 命令
https://blog.csdn.net/lqy971966/article/details/107006904
Linux grep 命令
https://blog.csdn.net/lqy971966/article/details/106927370
Linux awk 命令
https://blog.csdn.net/lqy971966/article/details/107019195

1. 定义

grep 全称是 Global Regular Expression Print
全局正则表达式打印

1.1 man grep //man 的解释

grep searches the named input FILEs for lines containing a match to the given PATTERN.
grep prints the matching lines.

1.2 功能:文件中内容查找

grep命令是一种强大的文本搜索工具(grep 根据文件的内容进行查找)。
它能使用正则表达式搜索文本,并把匹 配的行打印出来。

作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一
Linux sed 命令
https://blog.csdn.net/lqy971966/article/details/107006904

1.3 语法:grep [选项] ”模式“ [文件]

grep [OPTIONS] PATTERN [FILE…]
grep [选项] ”模式“ [文件]

1.4 grep、egrep、fgrep 等grep家族

grep家族包括grep、egrep和fgrep。
egrep和fgrep的命令只跟grep有很小不同。
egrep是grep的扩展,支持更多的re元字符。
fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,
也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。

2. 参数说明及示例

参数 说明
-c 计算找到‘搜索字符串’的行数
-o 指数出匹配的内容
-w 只匹配整个单词
-i 不区分大小写
-n 显示匹配内容的行号
-r 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作
-v 反向选择,即没有‘搜索字符串’内容的行
-l 列出文件内容符合指定的范本样式的文件名称
-E 扩展 grep,即 egrep,可以使用扩展正则表达式
–color=auto 搜索关键词显示颜色
-V 显示软件版本信息
-A 显示匹配后和它后面的n行
-B 显示匹配后和它前面的n行
-C 匹配行和它前后各n行

我们以下面这个为例子说明参数

grep_test.txt

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
HANI IS A GOOD MAN.
hani is a good man.
Two lines above this line is empty.
And this is the last line.

2.1 grep “is” grep_test.txt //grep xxx file [普通搜索 默认区分大小写]

root@ubuntu-admin-a1:/home/grepTest# grep "is" grep_test.txt 
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
hani is a good man.
Two lines above this line is empty.
And this is the last line.

2.2 grep -w “is” grep_test.txt // -w [只匹配整个单词]

root@ubuntu-admin-a1:/home/grepTest# grep -w is grep_test.txt 
this line is the 1st lower case line in this file.
hani is a good man.
Two lines above this line is empty.
And this is the last line.

2.3 grep -w -i “is” grep_test.txt // -i --ignore case [忽略大小写]

root@ubuntu-admin-a1:/home/grepTest# grep -w -i is grep_test.txt 
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
HANI IS A GOOD MAN.
hani is a good man.
Two lines above this line is empty.
And this is the last line.

2.4 grep -c “is” grep_test.txt // -c [计算匹配行数]

root@ubuntu-admin-a1:/home/grepTest# grep -c is grep_test.txt 
5

2.5 grep “is” grep_test.txt grep2_test.txt // 多文件查找

root@ubuntu-admin-a1:/home/grepTest# grep is grep_test.txt grep2_test.txt 
grep_test.txt:this line is the 1st lower case line in this file.
grep_test.txt:This Line Has All Its First Character Of The Word With Upper Case.
grep_test.txt:hani is a good man.
grep_test.txt:Two lines above this line is empty.
grep_test.txt:And this is the last line.
grep2_test.txt:hani is a good blog.
grep2_test.txt:Sharing is in this file.

2.6 grep -w -n “is” grep_test.txt // -n [显示行号]

root@ubuntu-admin-a1:/home/grepTest# grep -w -n is grep_test.txt 
2:this line is the 1st lower case line in this file.
5:hani is a good man.
6:Two lines above this line is empty.
7:And this is the last line.

2.7 grep -v “is” grep_test.txt // -v --invert [除了is之外的内容(排除输出)]

root@ubuntu-admin-a1:/home/grepTest# grep -v is grep_test.txt 
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
HANI IS A GOOD MAN.

如:
ps -ef | grep "abc" | grep -v "grep"  //寻找进程abc除了grep本身之外,肯定只有一个,否则是两个

2.7.1 ps -ef | grep “abc” | grep -v “grep” //寻找进程abc除了grep本身之外,肯定只有一个,否则是两个

2.8 grep “^th” grep_test.txt // ^ [正则表达式搜索]

root@ubuntu-admin-a1:/home/grepTest# grep "^th" grep_test.txt 
this line is the 1st lower case line in this file.

2.9 grep -A 2 “good” grep_test.txt // -A [显示匹配后和它后面的n行]

root@ubuntu-admin-a1:/home/grepTest# grep -A 2 "good" grep_test.txt 
hani is a good man.
Two lines above this line is empty.
And this is the last line.

2.10 grep -B 2 “good” grep_test.txt // -B [显示匹配后和它前面的n行]

root@ubuntu-admin-a1:/home/grepTest# grep -B 2 "good" grep_test.txt 
This Line Has All Its First Character Of The Word With Upper Case.
HANI IS A GOOD MAN.
hani is a good man.

2.11 grep -C 2 “good” grep_test.txt // -C [匹配行和它前后各n行]

root@ubuntu-admin-a1:/home/grepTest# grep -C 2 "good" grep_test.txt 
This Line Has All Its First Character Of The Word With Upper Case.
HANI IS A GOOD MAN.
hani is a good man.
Two lines above this line is empty.
And this is the last line.

2.12 获取最后的匹配项 grep 123abcd.log | tail -n 2

[root@glusterfs ]# grep AllTaskNum msg_debug.log | tail -n 2
[2020-11-13 08:59:25.448] [xxxc.c:1328] ---- Statistics: 	AllTaskNum=3746, SucTaskNum=3745 ----
[2020-11-13 08:59:27.475] [xxxc.c:1328] ---- Statistics: 	AllTaskNum=3746, SucTaskNum=3746 ----
[root@glusterfs ]# 

如果查找的内容比较长,请把数字改大一些,不然无法找到对应的查找内容

grep ‘name’ -A 10 显示匹配内容和后面的10行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值