0
点赞
收藏
分享

微信扫一扫

一、Linux grep

Linux命令之grep

1.grep介绍

linux命令grep--grep:Global search Regular Expression and Print out the line,该命令用于查找出文件里符合条件的字符串或正则表达式的行。

grep是用来进行文件检索的命令之一,在linux运维过程中,使用的非常频繁,建议一定记住一定要多多使用一定要熟练记忆!!!!

2.grep用法 

grep [参数] "pattern" filename

参数:

grep命令常用的参数

grep常用参数

参数

说明

-A

显示匹配指定内容及其之后(after)的n行

-B

显示匹配指定内容及其之前(before)的n行

-C

显示匹配指定内容前后各n行

-a

将二进制视为文件文件进行查找

-c

只统计匹配的行

-n

显示匹配行及行号

-i

忽略大小写

-v

反向查找

-w

按照单词进行过滤匹配

-m

匹配结果最多展示m条

-F

使正则失效

-r

递归查找

-E

使用扩展样式进行查找,等价于egrep

-o

只输出匹配内容

-l

列出包含匹配项的文件名

-L

列出不包含匹配项的文件名

pattern:

grep命令pattern匹配常用的正则表达式

常用的正则表达式匹配

表达式

说明

^

匹配开头,如 "^ztj" 即匹配以ztj开头的单词

$

匹配结尾,如 "ztj$" 即匹配以ztj结尾的单词

^$

匹配空行

.

英文"点",匹配任意一个且只有一个字符,不能匹配空行

\

转义字符

*

匹配前一个字符连续出现0次或1次以上

.*

匹配任意字符

^.*

匹配任意多个字符开头的内容

.*$

匹配任意多个字符结尾的内容

匹配0个或1个字符

+

匹配1个及以上字符

[abc]

匹配 [] 内集合中的任意一个字符,a或b或c,也可以写成 [a-c]

[^abc]

指定范围外的任意单个字符,[]内 ^ 表示取反操作

\{m\}

匹配m

\{m,n\}

至少m,至多n

\{m,\}

至少m

\{0,n\}

至多n

[]

指定范围内的任意单个字符

[0-9]

or

[[:digit:]]

数字

[a-z]

or

[[:lower:]]

小写字母

[A-Z]

or

[[:upper:]]

大写字母

[[:alpha:]]

所有字母,不区分大小写

[[:alnum:]]

所有字母和数字

[[:space:]]

所有带空格字符的

[[:punct:]]

所有含有特殊字符的

3.实例

3.1.打印匹配行的后5行

命令:

grep -A 5 "pwpolicy" anaconda-ks.cfg

[root@rhel77 ~]# grep -A 5 "pwpolicy" anaconda-ks.cfg 
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@rhel77 ~]#

3.2.打印匹配行的前5行

命令:

grep -B 5 "pwpolicy" anaconda-ks.cfg

[root@rhel77 ~]# grep -B 5 "pwpolicy" anaconda-ks.cfg 
%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

3.3.打印匹配行的前后5行

命令:

grep -A 5 -B 5 "pwpolicy" anaconda-ks.cfg

[root@rhel77 ~]# grep -A 5 -B 5 "pwpolicy" anaconda-ks.cfg 
%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@rhel77 ~]#

OR

grep -C 5 "pwpolicy" anaconda-ks.cfg

[root@rhel77 ~]# grep -C 5 "pwpolicy" anaconda-ks.cfg 
%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@rhel77 ~]#

OR

grep -5 "pwpolicy" anaconda-ks.cfg

[root@rhel77 ~]# grep -5 "pwpolicy" anaconda-ks.cfg
%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@rhel77 ~]#

3.4.统计pwpolicy出现的次数

命令:

grep -c "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -c "pwpolicy" anaconda-ks.cfg 
3
[root@rhel77 ~]#

3.5.列出pwpolicy所在行

命令:

grep -n "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -n "pwpolicy" anaconda-ks.cfg 
52:pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
53:pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
54:pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

3.6.列出pwpolicy所在行,忽略大小写

命令:

grep -ni "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -ni "pwpolicy" anaconda-ks.cfg 
52:pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
53:pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
54:pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

3.7.列出不包含pwpolicy的行

命令:

grep -v "pwpolicy" anaconda-ks.cfg

[root@rhel77 ~]# grep -v "pwpolicy" anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=static --device=ens33 --gateway=192.168.10.1 --ip=192.168.10.110 --netmask=255.255.255.0 --onboot=off --ipv6=auto --no-activate
network  --hostname=rhel77

repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
# Root password
rootpw --iscrypted $6$EVbItnP7747AenVw$lHPkryuzGj7098ZsDrVIolzIAjQ4qH6vc35c0ZkbjjN3Ie.XajcAv6s2GCIZ3YFl3gO25aa1Sr1ZeDY6QGbK4/
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part pv.630 --fstype="lvmpv" --ondisk=sda --size=10242
part swap --fstype="swap" --ondisk=sda --size=10240
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=70650
part /home --fstype="xfs" --ondisk=sda --size=10240
volgroup rhel_rhel77 --pesize=4096 pv.630
logvol /var  --fstype="xfs" --size=10240 --name=var --vgname=rhel_rhel77

%packages
@^minimal
@core

%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
%end
[root@rhel77 ~]#

3.8.精确匹配pwpolicy,并输出匹配结果

命令:

grep -w "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -w "pwpolicy" anaconda-ks.cfg 
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

3.9.匹配pwpolicy,匹配结果最多展示2条

命令:

grep -m 2 "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -m 2 "pwpolicy" anaconda-ks.cfg 
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
[root@rhel77 ~]#

3.10.匹配[89]字段,并输出匹配结果

命令:

grep -F "[89]" anaconda-ks.cfg 

[root@rhel77 ~]# grep -F "[89]" anaconda-ks.cfg 
# 2[89]0 ztj test
[root@rhel77 ~]#

3.11.模糊匹配8或9数值,并输出匹配结果

命令:

grep "[89]"  anaconda-ks.cfg

[root@rhel77 ~]# grep "[89]" anaconda-ks.cfg 
# 2[89]0 ztj test
lang en_US.UTF-8
network  --bootproto=static --device=ens33 --gateway=192.168.10.1 --ip=192.168.10.110 --netmask=255.255.255.0 --onboot=off --ipv6=auto --no-activate
rootpw --iscrypted $6$EVbItnP7747AenVw$lHPkryuzGj7098ZsDrVIolzIAjQ4qH6vc35c0ZkbjjN3Ie.XajcAv6s2GCIZ3YFl3gO25aa1Sr1ZeDY6QGbK4/
volgroup rhel_rhel77 --pesize=4096 pv.630
[root@rhel77 ~]#

3.12.只输出匹配pwpolicy字段

命令:

grep -o "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -o "pwpolicy" anaconda-ks.cfg 
pwpolicy
pwpolicy
pwpolicy
[root@rhel77 ~]#

3.13.输出包含pwpolicy字段的文件名

命令:

grep -l "pwpolicy" anaconda-ks.cfg 

[root@rhel77 ~]# grep -l "pwpolicy" anaconda-ks.cfg 
anaconda-ks.cfg
[root@rhel77 ~]#

3.14.输出不包含pwpolicy字段的文件名

命令:

grep -L "pwpolicy" *

[root@rhel77 ~]# grep -L "pwpolicy" *
1
a.txt
grep: conf: Is a directory
conf
grep: data: Is a directory
data
fork.sh
grep: inf: Is a directory
inf
lanmp.sh
lanmp_v3.2.tar.gz
grep: lib: Is a directory
lib
login-1.sh
login.sh
mysql_installation.sh
remi-release-7.rpm
shell_id.sh
stderr.txt
stdin.txt
stdin.txt 
grep: web1: Is a directory
web1
grep: web2: Is a directory
web2
grep: web3: Is a directory
web3
grep: web4: Is a directory
web4
grep: web5: Is a directory
web5
grep: web6: Is a directory
web6
[root@rhel77 ~]#

3.15.查找包含pwpolicy或part字段的行

命令:

grep -E "pwpolicy|part" anaconda-ks.cfg

[root@rhel77 ~]# grep -E "pwpolicy|part" anaconda-ks.cfg 
clearpart --none --initlabel
# Disk partitioning information
part pv.630 --fstype="lvmpv" --ondisk=sda --size=10242
part swap --fstype="swap" --ondisk=sda --size=10240
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=70650
part /home --fstype="xfs" --ondisk=sda --size=10240
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

OR

grep "pwpolicy\|part" anaconda-ks.cfg 
 

[root@rhel77 ~]# grep "pwpolicy\|part" anaconda-ks.cfg 
clearpart --none --initlabel
# Disk partitioning information
part pv.630 --fstype="lvmpv" --ondisk=sda --size=10242
part swap --fstype="swap" --ondisk=sda --size=10240
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=70650
part /home --fstype="xfs" --ondisk=sda --size=10240
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

3.16.输出以pwpolicy开头的行 

命令:

grep ^pwpolicy anaconda-ks.cfg

[root@rhel77 ~]# grep ^pwpolicy anaconda-ks.cfg 
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@rhel77 ~]#

3.17.输出以1024结尾的行

命令:

grep 1024$ anaconda-ks.cfg

[root@rhel77 ~]# grep 1024$ anaconda-ks.cfg 
part /boot --fstype="xfs" --ondisk=sda --size=1024
[root@rhel77 ~]#

3.18.匹配结尾为数字的行

命令:

grep [0-9]$ anaconda-ks.cfg

[root@rhel77 ~]# grep [0-9]$ anaconda-ks.cfg 
auth --enableshadow --passalgo=sha512
lang en_US.UTF-8
network  --hostname=rhel77
part pv.630 --fstype="lvmpv" --ondisk=sda --size=10242
part swap --fstype="swap" --ondisk=sda --size=10240
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=70650
part /home --fstype="xfs" --ondisk=sda --size=10240
volgroup rhel_rhel77 --pesize=4096 pv.630
logvol /var  --fstype="xfs" --size=10240 --name=var --vgname=rhel_rhel77
[root@rhel77 ~]#

3.19.列出anaconda-ks.cfg文件中,包含2位数或3位数的内容

命令:

grep -n -E "\<[0-9]{2,3}\>" anaconda-ks.cfg 

[root@rhel77 ~]# grep -n -E "\<[0-9]{2,3}\>" anaconda-ks.cfg 
7:# 2[89]0 ztj test
18:network  --bootproto=static --device=ens33 --gateway=192.168.10.1 --ip=192.168.10.110 --netmask=255.255.255.0 --onboot=off --ipv6=auto --no-activate
34:part pv.630 --fstype="lvmpv" --ondisk=sda --size=10242
39:volgroup rhel_rhel77 --pesize=4096 pv.630
[root@rhel77 ~]#

3.20.找出anaconda-ks.cfg文件中数值7出现的次数至少2次

命令:

grep -E '7{2,}' anaconda-ks.cfg 

[root@rhel77 ~]# grep -E '7{2,}' anaconda-ks.cfg 
network  --hostname=rhel77
rootpw --iscrypted $6$EVbItnP7747AenVw$lHPkryuzGj7098ZsDrVIolzIAjQ4qH6vc35c0ZkbjjN3Ie.XajcAv6s2GCIZ3YFl3gO25aa1Sr1ZeDY6QGbK4/
volgroup rhel_rhel77 --pesize=4096 pv.630
logvol /var  --fstype="xfs" --size=10240 --name=var --vgname=rhel_rhel77
[root@rhel77 ~]#

3.21.递归找出当前etc目录下所有文件中开头包含root的文件

命令:

grep -r '^root' /etc

[root@rhel77 ~]# grep -r '^root' /etc
/etc/pki/ca-trust/extracted/README:root CA certificates.
/etc/pki/ca-trust/extracted/java/README:root CA certificates.
/etc/pki/ca-trust/extracted/openssl/README:root CA certificates.
/etc/pki/ca-trust/extracted/pem/README:root CA certificates.
/etc/selinux/targeted/seusers:root:unconfined_u:s0-s0:c0.c1023
/etc/group-:root:x:0:
/etc/gshadow-:root:::
/etc/group:root:x:0:
/etc/gshadow:root:::
/etc/passwd-:root:x:0:0:root:/root:/bin/bash
/etc/shadow-:root:$6$EVbItnP7747AenVw$lHPkryuzGj7098ZsDrVIolzIAjQ4qH6vc35c0ZkbjjN3Ie.XajcAv6s2GCIZ3YFl3gO25aa1Sr1ZeDY6QGbK4/::0:99999:7:::
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/services:rootd           1094/tcp                # ROOTD
/etc/services:rootd           1094/udp                # ROOTD
/etc/shadow:root:$6$EVbItnP7747AenVw$lHPkryuzGj7098ZsDrVIolzIAjQ4qH6vc35c0ZkbjjN3Ie.XajcAv6s2GCIZ3YFl3gO25aa1Sr1ZeDY6QGbK4/::0:99999:7:::
/etc/security/limits.d/20-nproc.conf:root       soft    nproc     unlimited
/etc/sudoers:root	ALL=(ALL) 	ALL
[root@rhel77 ~]#

举报

相关推荐

0 条评论