首页 技术 正文
技术 2022年11月16日
0 收藏 680 点赞 4,451 浏览 3539 个字

grep

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

Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。

-color=auto 对匹配到的文本着色显示
-m x 匹配x次后停止
-v 显示不被pattern匹配到的行,即取反
-i 忽略字符大小写
-n 显示匹配的行号
-c 统计匹配的行数
-o 仅显示匹配到的字符串
-q 静默模式,不输出任何信息 可结合echo$?查看命令执行状态的返回值。返回值为0,表示成功执行
-A 3 after, 后3行
-B 3 before, 前3行
-C 3 context, 前后各3行
-e 实现多个选项间的逻辑or关系,如:grep –e '123' -e 'abc' file
-w 匹配整个单词
-E 使用ERE,相当于egrep
-F 不支持正则表达式,相当于fgrep grep -F 'r..t' /etc/passwd r..t会被认为是一个普通的字符串
-f file 根据模式文件处理 根据文件里的内容进行匹配 grep -f test.txt /etc/passwd
-r 递归目录,但不处理软链接 主要用于查找文件
-R 递归目录,但处理软链接

实例

排除掉文件中以#开头和文件中的空行
[root@localhost ~]# grep -v '^[[:space:]]*#' /etc/httpd/conf/httpd.conf | grep -v '^$'
[root@localhost ~]# grep -v '^[[:space:]]*#\|^$' /etc/httpd/conf/httpd.conf
[root@localhost ~]# grep -Ev '^[[:space:]]*#|^$' /etc/httpd/conf/httpd.conf [root@localhost ~]# grep -nA3 root /etc/passwd 后
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
-----------------------------------------------------------------------------------------------
[root@localhost ~]# grep -nB3 root /etc/passwd 前
1:root:x:0:0:root:/root:/bin/bash
--
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
-----------------------------------------------------------------------------------------------
[root@localhost ~]# grep -nC3 root /etc/passwd 前后
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin或
grep -e
[root@localhost ~]# grep -e root -e bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
yc:x:1000:1000:yc:/home/yc:/bin/bash
使用扩展的正则表达式 或使用egrep egrep "root|bash" /etc/passwd
[root@localhost ~]# grep -E "root|bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
yc:x:1000:1000:yc:/home/yc:/bin/bash-f 根据文件里的内容进行匹配
[root@localhost ~]# cat test.txt
r..t
bash
[root@localhost ~]# grep -f test.txt /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
yc:x:1000:1000:yc:/home/yc:/bin/bash统计连接数
[root@localhost ~]# grep -v '^State' ss.log | grep -Eo '^[[:alpha:]-]+\>' | sort | uniq -c
1 ESTAB
8 LISTEN统计连接的IP(Peer Address:Port)
[root@localhost ~]# ss -nt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 192.168.42.200:22 192.168.42.4:11654
ESTAB 0 0 192.168.42.200:22 192.168.42.4:11656
ESTAB 0 36 192.168.42.200:22 192.168.42.4:10380
ESTAB 0 0 192.168.42.200:22 192.168.42.4:11651 [root@localhost ~]# ss -nt | grep -v '^State' | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]+ *$'
192.168.42.4:11654
192.168.42.4:11656
192.168.42.4:10380
192.168.42.4:11651 [root@localhost ~]# ss -nt | grep -v '^State' | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]+ *$' | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | sort | uniq -c
4 192.168.42.4
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852