首页 Linux网维 linux-文本处理-grep

linux-文本处理-grep

文本处理三剑客之 grep grep: Global search REgular expression and Print out the line 作用:文本搜索工具,根据用户…

文本处理三剑客之 grep

grep: Global search REgular expression and Print out the line

作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配到的行

模式:由正则表达式字符及文本字符所编写的过滤条件

格式:

grep [OPTIONS] PATTERN [FILE...]

常见选项:
–color=auto 对匹配到的文本着色显示
-m # 匹配#次后停止
-v 显示不被pattern匹配到的行
-i 忽略字符大小写
-n 显示匹配的行号
-c 统计匹配的行数
-o 仅显示匹配到的字符串
-q 静默模式,不输出任何信息
-A # after, 后#行
-B # before, 前#行
-C # context, 前后各#行
-e 实现多个选项间的逻辑or关系,如:grep –e ‘cat ’ -e ‘dog’ file
-w 匹配整个单词
-E 使用ERE,相当于egrep
-F 不支持正则表达式,相当于fgrep
-f file 根据模式文件处理
-r 递归目录,但不处理软链接
-R 递归目录,但处理软链接

范例:

grep root /etc/passwd
grep "USER"  /etc/passwd
grep 'USER'  /etc/passwd
grep whoami  /etc/passwd

范例:取两个文件的相同行

[root@centos8 ~]#cat /data/f1.txt
a
b
1
c
[root@centos8 ~]#cat /data/f2.txt
b
e
f
c
1
2
[root@centos8 ~]#grep -f /data/f1.txt  /data/f2.txt
b
c
1

范例:

df | grep '^/dev/sd' |tr -s ' ' %|cut -d% -f5|sort -n|tail -1

范例:

[root@centos8 ~]#ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n3
      3 10.0.0.1
      1 172.16.4.100
      1 172.16.31.188

范例:

[root@centos8 ~]#grep -v "^#" /etc/profile | grep -v '^$'
[root@centos8 ~]#grep -v "^#\|^$" /etc/profile
[root@centos8 ~]#grep -v "^\(#\|$\)" /etc/profile 
[root@centos8 ~]#grep -Ev "^(#|$)" /etc/profile
[root@centos8 ~]#egrep -v "^(#|$)" /etc/profile
[root@centos6 ~]#egrep -v '^(#|$)' /etc/httpd/conf/httpd.conf

范例:

[root@centos8 ~]#grep -o  'r..t' /etc/passwd
root
root
root
root
r/ft
rypt

范例:

[root@centos8 ~]#ifconfig | grep -E '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
        inet 10.0.0.8  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 172.16.0.123  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::c11e:4792:7e77:12a4  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
[root@centos8 ~]#ifconfig | grep -E '([0-9]{1,3}.){3}[0-9]{1,3}'
        inet 10.0.0.8  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 172.16.0.123  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::c11e:4792:7e77:12a4  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0

[root@centos8 ~]#ifconfig eth0 | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
10.0.0.8

[root@centos8 ~]#cat regex.txt 
([0-9]{1,3}\.){3}[0-9]{1,3}
[root@centos8 ~]#ifconfig | grep -oEf regex.txt 
10.0.0.8
255.255.255.0
10.0.0.255
127.0.0.1
255.0.0.0

范例:

[root@centos8 ~]#grep -E 'root|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
xiaoming:x:1002:1002::/home/xiaoming:/bin/bash
roob:x:1003:1003::/home/roob:/bin/bash
[root@centos8 ~]#grep -e 'root' -e 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
xiaoming:x:1002:1002::/home/xiaoming:/b

范例:

[root@centos8 ~]#grep -w root  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos8 ~]#grep  '\<root\>'  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

范例:

[root@centos8 ~]#grep "^\(.*\)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin

[root@centos8 ~]#grep -E "^(.*)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin

[root@centos8 ~]#egrep  "^(.*)\>.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin

范例:面试题,算出所有人的年龄总和

[root@centos8 ~]#cat /data/nianling.txt
xiaoming=20
xiaohong=18
xiaoqiang=22

[root@centos8 ~]#cut -d"=" -f2 /data/nianling.txt|tr '\n' + | grep -Eo ".*[0-9]"|bc
60
[root@centos8 ~]#grep -Eo "[0-9]+" /data/nianling.txt | tr '\n' + | grep -Eo ".*[0-9]"|bc
60
免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

作者: 3182235786a

为您推荐

CentOS系统升级内核版本教程,centos内核版本升级

CentOS系统升级内核版本教程,centos内核版本升级

CentOS 系统升级系统内核版本  1、显示CentOS 系统内核版本:             [root@demo...
Linux下对 Nginx SSL 的性能进行调整

Linux下对 Nginx SSL 的性能进行调整

初始化服务器   这个web服务器运行在一个EC2 t1.micro 环境.我选择 Nginx + PHP5-FPM 来...
Linux Shell经典实例解析:Oracle启动脚本

Linux Shell经典实例解析:Oracle启动脚本

Oracle的启动脚本从功能上讲主要分为两个部分,第一部分是初始化各种环境变量,以确认当前Oracle服务器的版本,从而...
centOS6.4 64位下安装nfs文件共享系统

centOS6.4 64位下安装nfs文件共享系统

不知道谁装的服务器,默认自带,以下内容摘自互联网,配置部分按教程执行成功 一、环境介绍:   服务器:centos 19...
Linux下的Samba安装配置

Linux下的Samba安装配置

1 Samba 简介 Samba(SMB是其缩写) 是一个网络服务器,用于Linux和Windows共享文件之用;Sam...

发表回复

返回顶部