Linux-grep命令

简介: Linux-grep命令

概述

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

语法

grep命令常见用法

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

grep match_pattern file_name 
grep "match_pattern" file_name

在多个文件中查找

grep "match_pattern" file1 file2 ...

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

grep -v "match_pattern" file_name

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

grep "match_pattern" file_name --color=auto

使用正则表达式 -E 选项

grep -E "[1-9]+" 
egrep "[1-9]+"

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

[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 选项

[root@entel2 test]# cat args.txt 
aaa
bbb
ccc
bbb
ddd
aaaaa
[root@entel2 test]# grep -c "aaa" args.txt 
2

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

[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

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

[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 一般总是配合使用。

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

[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递归搜索文件

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

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

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

[root@entel2 ~]# echo "hello world" | grep -i "HELLO"
hello world

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

[root@entel2 ~]# echo this is a text line | grep -e "is" -e "line" -o
is
is
line

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

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

grep静默输出

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


相关文章
|
8天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
22天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
34 11
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
143 8
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
206 20
|
2月前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
129 7
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
100 14
Linux 10 个“who”命令示例
|
3月前
|
监控 网络协议 Linux
Linux netstat 命令详解
Linux netstat 命令详解
|
3月前
|
Linux 数据库
Linux中第一次使用locate命令报错?????
在Linux CentOS7系统中,使用`locate`命令时出现“command not found”错误,原因是缺少`mlocate`包。解决方法是通过`yum install mlocate -y`或`apt-get install mlocate`安装该包,并执行`updatedb`更新数据库以解决后续的“can not stat”错误。
62 9
|
3月前
|
运维 监控 网络协议
运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面
本文介绍了运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面,旨在帮助读者提高工作效率。从基本的文件查看与编辑,到高级的网络配置与安全管理,这些命令是运维工作中的必备工具。
297 3
|
3月前
|
Linux
在 Linux 系统中,`find` 命令
在 Linux 系统中,`find` 命令
57 1