前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux-grep命令

Linux-grep命令

作者头像
小小工匠
发布2021-08-16 11:00:43
4.9K0
发布2021-08-16 11:00:43
举报
文章被收录于专栏:小工匠聊架构

概述

grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

语法

这里写图片描述
这里写图片描述

grep命令常见用法

在文件中搜索一个单词,命令会返回一个包含“match_pattern”的文本行

代码语言:javascript
复制
grep match_pattern file_name 
grep "match_pattern" file_name

在多个文件中查找

代码语言:javascript
复制
grep "match_pattern" file1 file2 ...

输出除匹配项之外的所有行 -v 选项

代码语言:javascript
复制
grep -v "match_pattern" file_name

标记匹配颜色 –color=auto 选项

代码语言:javascript
复制
grep "match_pattern" file_name --color=auto

使用正则表达式 -E 选项

代码语言:javascript
复制
grep -E "[1-9]+" 
或 
egrep "[1-9]+"

只输出文件中匹配到的部分 -o 选项

代码语言:javascript
复制
[root@entel2 test]# echo this is a test line. | grep -o -E "[a-z]+\."
line.

[root@entel2 test]# echo this is a test line. | egrep  -o  "[a-z]+\."
line.

统计文件或者文本中包含匹配字符串的行数 -c 选项

代码语言:javascript
复制
[root@entel2 test]# cat args.txt 
aaa
bbb
ccc
bbb
ddd
aaaaa
[root@entel2 test]# grep -c "aaa" args.txt 
2

输出包含匹配字符串的行数 -n 选项

代码语言:javascript
复制
[root@entel2 test]# grep -n "aaa" args.txt 
1:aaa
6:aaaaa
[root@entel2 test]# cat args.txt |grep -n "aaa"
1:aaa
6:aaaaa

#多个文件 
grep "match pattern" -n file_1 file_2

打印样式匹配所位于的字符或字节偏移

代码语言:javascript
复制
[root@entel2 test]# echo gun is not unix | grep -b -o "not"
7:not
[root@entel2 test]# 
[root@entel2 test]# echo gun is not unix | grep -b -o "is"
4:is
[root@entel2 test]# echo gun is not unix | grep -b -o "gun"
0:gun
[root@entel2 test]# echo gun is not unix | grep -b -o "i"
4:i
13:i

一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。 选项 -b -o 一般总是配合使用。

搜索多个文件并查找匹配文本在哪些文件中

代码语言:javascript
复制
[root@entel2 test]# cat grep1.txt 
xiaogongjiang
xiaogongjiang1
[root@entel2 test]# cat grep2.txt 
xiaogongjiang
this is just for test grep -l
[root@entel2 test]# grep -l "xiaogongjiang" grep1.txt  grep2.txt 
grep1.txt
grep2.txt
[root@entel2 test]# grep -l "xiaogongjiang" grep*
grep1.txt
grep2.txt
[root@entel2 test]# grep -l "test" grep*
grep2.txt
[root@entel2 test]# 

grep递归搜索文件

在多级目录中对文本进行递归搜索

代码语言:javascript
复制
# .表示当前目录。
[root@entel2 ~]# grep  "xiaogongjiang"  . -n -r 
./test/grep1.txt:1:xiaogongjiang
./test/grep1.txt:2:xiaogongjiang1
./test/grep2.txt:1:xiaogongjiang

忽略匹配样式中的字符大小写

代码语言:javascript
复制
[root@entel2 ~]# echo "hello world" | grep -i "HELLO"
hello world

选项 -e 制动多个匹配样式

代码语言:javascript
复制
[root@entel2 ~]# echo this is a text line | grep -e "is" -e "line" -o
is
is
line
这里写图片描述
这里写图片描述

在grep搜索结果中包括或者排除指定文件

这里写图片描述
这里写图片描述

使用0值字节后缀的grep与xargs

这里写图片描述
这里写图片描述

grep静默输出

这里写图片描述
这里写图片描述

打印出匹配文本之前或者之后的行

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/09/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 语法
  • grep命令常见用法
    • 在文件中搜索一个单词,命令会返回一个包含“match_pattern”的文本行
      • 在多个文件中查找
        • 输出除匹配项之外的所有行 -v 选项
          • 标记匹配颜色 –color=auto 选项
            • 使用正则表达式 -E 选项
              • 只输出文件中匹配到的部分 -o 选项
                • 统计文件或者文本中包含匹配字符串的行数 -c 选项
                  • 输出包含匹配字符串的行数 -n 选项
                    • 打印样式匹配所位于的字符或字节偏移
                      • 搜索多个文件并查找匹配文本在哪些文件中
                        • grep递归搜索文件
                          • 忽略匹配样式中的字符大小写
                            • 选项 -e 制动多个匹配样式
                              • 在grep搜索结果中包括或者排除指定文件
                                • 使用0值字节后缀的grep与xargs
                                  • grep静默输出
                                    • 打印出匹配文本之前或者之后的行
                                    领券
                                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档