文本三剑客——grep过滤

简介: 文本三剑客——grep过滤

1、grep的基本使用命令

grep:过滤 通用的正则表达式分析程序

grep、egrep、fgrep:做匹配来过滤的

pattern 模式---》模板   可以接受一个正则表达式

用途:在文件中查找并显示包含指定字符串的行。

格式:grep [选项].... 模式 目标文件

grep -i  查找时忽略大小写
grep -v  反转查找 过滤出没有的
grep -n 显示符合模式要求的行号。
grep -o 显示匹配的行的内容
grep -r 递归搜索所有文件以及目录下的所有子文件。(当前目录下)
grep -E 支持更多的元字符(支持扩展正则)
grep -A 找到匹配的行以及后几行
grep -B 输出匹配的行以及前几行

grep -i :查找时忽略大小写。

[root@nginx-kafka01 opt]#  ps -ef |grep -i network
root       6144      1  0 10:57 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
root       9826   8828  0 22:26 pts/2    00:00:00 grep --color=auto -i network

grep -v 反转查找 过滤出没有的。

[root@localhost ~]# grep -v sbin /etc/passwd   #过滤出不含sbin的行
root:x:0:0:root:/root:/bin/bash
angel:x:1000:1000::/home/angel:/bin/bash
aj:x:1001:1001::/home/aj:/bin/bash
qiantao:x:1002:1002::/home/qiantao:/bin/bash
yalin:x:1003:1003::/home/yalin:/bin/bash
jj:x:1004:1004::/home/jj:/bin/bash
.....部分截取
[root@localhost ~]# grep sbin /etc/passwd   #过滤出含有sbin的行。
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

查找sshd_config里除了空行以及以#开头以外的行。

egrep -v  "^#|^$" /etc/ssh/sshd_config

[root@localhost ssh]# egrep -v  "^#|^$" sshd_config

grep -n :显示符合模式要求的行号。

[root@nginx-kafka01 opt]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
27:apache:x:48:48:Apache:/opt/rh/httpd24/root/usr/share/httpd:/sbin/nologin
[root@nginx-kafka01 opt]# grep -o sbin /etc/passwd

grep -o:只显示匹配的行的内容。

[root@nginx-kafka01 opt]# grep -o sbin /etc/passwd
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin

grep -r:递归搜索所有文件以及目录下的所有子文件。(当前目录下)

[root@nginx-kafka01 opt]# cd /etc/sysconfig/
[root@nginx-kafka01 sysconfig]# pwd
/etc/sysconfig
[root@nginx-kafka01 sysconfig]# grep en *
anaconda:# This file has been generated by the Anaconda Installer 21.48.22.147-1
authconfig:FAILLOCKARGS="deny=4 unlock_time=1200"
grep: cbq: 是一个目录
grep: console: 是一个目录
cpupower:CPUPOWER_START_OPTS="frequency-set -g performance"
cpupower:CPUPOWER_STOP_OPTS="frequency-set -g ondemand"
crond:# CRONDARGS= :  any extra command-line startup arguments for crond
ebtables-config:# If EBTABLES_BINARY_FORMAT="no" then restoring the firewall rules
ebtables-config:# Save current firewall rules on stop.
ebtables-config:# Save current firewall rules on restart.
ebtables-config:# Save rule counters when saving a kernel table to a file. If the
ebtables-config:# rule counters were saved, they will be restored when restoring the table.
grub:GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
init:# terminal sequence to move to that column. You could change this
init:MOVE_TO_COL="echo -en \\033[${RES_COL}G"
init:# terminal sequence to set color to a 'success' color (currently: green)
init:SETCOLOR_SUCCESS="echo -en \\033[0;32m"
init:# terminal sequence to set color to a 'failure' color (currently: red)
init:SETCOLOR_FAILURE="echo -en \\033[0;31m"
init:# terminal sequence to set color to a 'warning' color (currently: yellow)
init:SETCOLOR_WARNING="echo -en \\033[0;33m"
init:# terminal sequence to reset to the default color.
init:SETCOLOR_NORMAL="echo -en \\033[0;39m"
加-r也能查找,递归查找当前目录下的所有文件里面包含里面目录下的子文件,匹配含有ens的行。
[root@nginx-kafka01 sysconfig]# grep ens * -r  
kdump:#What is the images extension.  Relocatable kernels don't have one
network-scripts/ifdown-eth:# it under the terms of the GNU General Public License, version 2,
network-scripts/ifdown-eth:# GNU General Public License for more details.
network-scripts/ifdown-eth:# You should have received a copy of the GNU General Public License
network-scripts/ifdown-tunnel:# it under the terms of the GNU General Public License, version 2,
network-scripts/ifdown-tunnel:# GNU General Public License for more details.
network-scripts/ifdown-tunnel:# You should have received a copy of the GNU General Public License
network-scripts/ifup-eth:# it under the terms of the GNU General Public License, version 2,
network-scripts/ifup-eth:# GNU General Public License for more details.
network-scripts/ifup-eth:# You should have received a copy of the GNU General Public License
network-scripts/ifup-eth:# Bonding initialization. For DHCP, we need to enslave the devices early,
network-scripts/ifup-eth:    # add the bits to setup the needed post enslavement parameters
network-scripts/ifup-ipv6:# Enable IPv6 RFC3041 privacy extensions if desired
network-scripts/ifup-tunnel:# it under the terms of the GNU General Public License, version 2,
network-scripts/ifup-tunnel:# GNU General Public License for more details.
network-scripts/ifup-tunnel:# You should have received a copy of the GNU General Public License
network-scripts/ifup-wireless:# it under the terms of the GNU General Public License, version 2,
network-scripts/ifup-wireless:# GNU General Public License for more details.
network-scripts/ifup-wireless:# You should have received a copy of the GNU General Public License
network-scripts/network-functions:            awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,

grep sbin -n /etc/passwd #找到/etc/passwd里面的sbin 所在行,并显示行号

[root@nginx-kafka01 sysconfig]# grep sbin -n /etc/passwd
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
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt

grep -A:找到匹配的行以及后几行

grep -B:输出匹配的行以及前几行

# -A 2:找到ftp 所在的第二行以及后几行,-n:显示行号
[root@nginx-kafka01 sysconfig]# grep ftp -n -A 2 /etc/passwd  
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
14-systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
#-B 2;找到第二行以及前几行
[root@nginx-kafka01 sysconfig]# grep ftp -n -B 2 /etc/passwd  
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

2、grep使用正则表达式

2.1 正则的定义

使用一些特殊符号+字母和数字按照某个正确的规则组合成一个公式来表示某个意思这就是正则表达式。

有什么用:用来查找,根据正则表达式表达出来的意思,去海量的文本里面查找符合这个意思的内容。

很多命令都有查找功能:vim、grep、sed、awk等。

grep过滤,通用的正则表达式分析程序。

正则表达式:regular expression  ---re。过滤文本的规则。

^aa 表示以aa开头的行
aa$ 表示以aa结尾的行
[] 表示一个字符集
[a-z] 从a-z中取一个
[^a-z] 不取a-z的字符,过滤不包含字母的行,但是前提是要以字母开头,不以字母开头,即使包含字母也会被输出。

2.2 通配符

[abc]:表示abc中的任意一个字符。

[^abc]:不含abc中的任意一个。

通配符:在shell里的叫法。

* ? .的含义:

. 表示任意一个字符
* 代表任意个任意字符
? 代表任意一个字符串

元字符: 正则表达式。

元字符:用来描述其他字符,有特殊意义的字符。

meta character

^ 表示以....开头
$ 表示以...结尾
. 表示一个占位符
前面的字符串出现0次或者1次。
* 前面的字符串出现0次或者多次。

+

|

前面的字符串出现1次或多次。 

{n,m}

匹配前一项n到m次

{n}

匹配n次

{,m}

匹配前一项0到m次

{n,}

匹配前一项n次以上

分组捕获:分组向后引用,()括起来的表示分组 \1 \2b表示第一个分组和第二个分组获取到的内容。

正则:

1、基本正则:元字符比较少 ^ $ ? *

2、扩展正则 : | + {}

grep -E = egrep

grep [OPTIONS] PATTERN [FILE...]

grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

grep, egrep fgerp - print lines matching a pattern

匹配模式在行里查找,然后输出--》输出匹配模式的行。

模式(pattern)是什么?

模式是grep里包含了正则表达式的一个模型(字符串、样例)

安装某个模型根据正则表达式去构成的一个公式。--表达一个意思

例如:匹配以wangshuai开头的三次,{0-9}表示数字0-9。:egrep “^wangshuai{3}{0-9}+ " ws.txt

写正则表达式。其实就是找共性的特点,也就是找规律。

字符串1@字符串2.字符串3:[0-Z_]+@[0-Z]+\.[a-Z]+

查找邮箱:

[root@localhost lianxi]# cat mail.txt
fengdeyong@163.com  fdffaf1212
695811769@qq.com  8989
luoziyao123@facebook.com fjdkfj
[root@localhost lianxi]# egrep "[0-Z_]+@[0-Z]+\.[a-Z]+" mail.txt
fengdeyong@163.com  fdffaf1212
695811769@qq.com  8989
luoziyao123@facebook.com fjdkfj

egrep -o :只显示匹配的字符串。

[root@localhost lianxi]# egrep -o  "[0-Z_]+@[0-Z]+\.[a-Z]+" mail.txt
fengdeyong@163.com
695811769@qq.com
luoziyao123@facebook.com

xargs:①将列转为一行输出;②把前一条传入的命令作为后一条的参数使用。

[root@localhost lianxi]# egrep -o  "[0-Z_]+@[0-Z]+\.[a-Z]+" mail.txt|xargs
fengdeyong@163.com 695811769@qq.com luoziyao123@facebook.com

grep ^[^a-zA-Z0-9_] grep_test.txt 显示不以字母、数字、下划线开头的行。

grep -v ^[a-zA-Z0-9_] grep _test.txt 不显示出显示不以字母、数字、下划线开头的行。

[root@localhost lianxi]# echo "a1a2"|egrep "([a-z][0-9])"
a1a2
[root@localhost lianxi]# echo "a1a2"|egrep "([a-z][0-9])\1"
[root@localhost lianxi]# echo "a1a1"|egrep "([a-z][0-9])\1"
a1a1
[root@localhost lianxi]# echo "a1a1bbccdd"|egrep "([a-z][0-9])\1bb([a-z])\2"
a1a1bbccdd
①egrep "b.*" grep-test.txt
②egrep "#.*" grep-test.txt 
③egrep "#.{2}*" grep-test.txt

前一项:

前一项
[root@localhost lianxi]# egrep "([a-z][0-9]) +" grep-test.txt 匹配一次及以上,括号是把他们作为一个整体
a1b2
[root@localhost lianxi]# grep ^[0-9] grep-test.txt  过滤出以数字开头的行^[0-9]
123
22
[root@localhost lianxi]# grep "[^0-9]" grep-test.txt       [^0-9]过滤出不以数字开头的行
[root@localhost lianxi]# grep ^# grep-test.txt   过滤出grep-test.txt 文本中以“^’开头的内容
#abc
[root@localhost lianxi]# grep b$ grep-test.txt   过滤出grep-test.txt 文本中以”b“结尾的内容
 #bbb
 #bbb
[root@localhost lianxi]# grep [a-z] grep-test.txt  过滤出grep-test.txt 文本中含有字母的内容
[root@localhost lianxi]# grep [a-Z] grep-test.txt  过滤出grep-test.txt 文本中大小写字母的内容
[root@localhost lianxi]# grep "[^a]" grep-test.txt  过滤出不以a开头的行
[root@localhost lianxi]# grep "[^0-9]" grep-test.txt
[root@localhost lianxi]# grep "[rs]" /etc/passwd   过滤出/etc/passwd中含有r或者s的行
[root@localhost lianxi]# grep "[^rs]" /etc/passwd    不含r或s的行
[root@localhost lianxi]# grep -E "1[0-9]{8,10}" grep-test.txt  -E 支持正则表达式, {8,10}8表示匹配前一项
18569628804
[root@localhost lianxi]# egrep "#.{2}"grep-test.txt   匹配#开头的两次
[root@localhost lianxi]# egrep "#.*"grep-test.txt
[root@localhost lianxi]# egrep "b.*" grep-test.txt
[root@localhost lianxi]# egrep "b*[a-z]" grep-test.txt
[root@localhost lianxi]# egrep "^#|^[0-9]" grep-test.txt    正则或: 匹配出以#开头或者数字开头的行
#abc
123
22
18569628804

\b 表示匹配词边界,单词的边界

\B 表示匹配非词边界

[root@localhost lianxi]# echo "#westaa" |egrep "\bwest"
#westaa
[root@localhost lianxi]# echo "#west" |egrep "west\b"
#west
[root@localhost lianxi]# echo "#westaa" |egrep "west\b"
[root@localhost lianxi]# echo "#westaa" |egrep "\bwest\B"
#westaa
[root@localhost lianxi]# echo "#westaa" |egrep "\Bwest\B"



相关文章
|
存储 监控 Linux
性能工具之linux三剑客awk、grep、sed详解
Linux 三剑客 awk,sed和grep 在性能领域广泛用于性能建模、性能监控及性能分析等方面,也是各大互联网公司测试岗高频面试题,中高端测试人员必备技能之一。
397 1
性能工具之linux三剑客awk、grep、sed详解
|
安全 Linux 网络虚拟化
关于容器云的三种网络设计
【2月更文挑战第9天】容器网络设计:隧道、路由、VLAN。
|
算法 编译器 C++
【C++ 格式化输出 】C++ 高级输出格式化:掌握 iostream 与 iomanip
【C++ 格式化输出 】C++ 高级输出格式化:掌握 iostream 与 iomanip
1156 1
|
8月前
|
安全 网络安全 网络虚拟化
采用默认配置通过IKE协商方式建立IPSec隧道示例
本文介绍了通过配置IPSec隧道实现分支与总部网络间流量安全保护的组网需求与操作步骤。分支网关RouterA和总部网关RouterB通过公网通信,子网分别为10.1.1.0/24和10.1.2.0/24。配置思路包括:设置接口IP地址与静态路由、定义ACL规则、配置IPSec安全提议、IKE对等体属性、安全策略以及在接口上应用策略组,确保数据流的安全传输。
采用默认配置通过IKE协商方式建立IPSec隧道示例
|
网络协议 算法 Linux
深度解密 TCP 三次握手与四次挥手
深度解密 TCP 三次握手与四次挥手
554 9
|
安全 应用服务中间件 网络安全
简单比较 http https http2,我们要如何把http升级为https
【9月更文挑战第13天】本文对比了HTTP、HTTPS和HTTP/2的特点与适用场景。HTTP以明文传输,适合低安全要求的环境;HTTPS通过SSL/TLS加密,适用于电子商务等安全要求高的场景;HTTP/2采用二进制格式和多路复用,适合高性能Web应用。文章还详细介绍了将HTTP升级为HTTPS的步骤,包括申请和安装SSL证书、配置Web服务器、重定向HTTP流量到HTTPS以及测试HTTPS功能。升级到HTTPS可提高数据安全性和用户信任度。
984 13
|
安全 Linux 调度
docker的底层原理三: 内核共享
本文阐述了Docker容器通过共享宿主机的内核来实现轻量级运行,同时利用命名空间、控制组、文件系统和网络隔离等技术确保容器的安全性和资源控制。
622 6
|
存储 数据管理 API
OpenStack的块存储卷管理快照与克隆
【8月更文挑战第27天】
505 4
|
Kubernetes Cloud Native 应用服务中间件
Kubernetes 自动伸缩策略:优化资源利用率
【8月更文第29天】在现代云原生环境中,应用的流量往往具有不可预测性。为了应对这种变化,Kubernetes 提供了多种自动伸缩机制来动态调整应用实例的数量和每个实例分配的资源。本文将深入探讨两种主要的自动伸缩工具:水平 Pod 自动伸缩器 (HPA) 和垂直 Pod 伸缩器 (VPA),并提供实际的应用示例。
426 1
|
负载均衡 算法 应用服务中间件
解密Nginx负载均衡:实现流量分发与故障转移
解密Nginx负载均衡:实现流量分发与故障转移
954 1