shell编程四剑客之GREP

shell编程四剑客之GREP

  • 全面搜索正则表达式(Global search regular expression(RE) ,GREP)是一种强大的文本搜索工具它能使用正则表达式搜索文本,并把匹配的行打印出来

  • Unix/Linux的grep家族包括grep、egrep和fgrep其中egrep和fgrep的命令跟grep有细微的区别egrep是grep的扩展,支持更多的re元字符, fgrep是fixed grep或fast grep简写,它们把所有的字母都看作单词,正则表达式中的元字符表示其自身的字面意义,不再有其他特殊的含义,一般使用比较少。

  • 目前Linux操作系统默认使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。其语法格式及常用参数详解如下:

  • grep -option ‘word’ Filename
    Grep常用参数详解如下:

-a以文本文件方式搜索;
-c计算找到的符合行的次数;
-i忽略大小写;
-n顺便输出行号;
-v反向选择,即显示不包含匹配文本的所有行;
-h查询多文件时不显示文件名;
-l查询多文件时只输出包含匹配字符的文件名;
-s不显示不存在或无匹配文本的错误信息;
-E允许使用egrep扩展模式匹配。
  • 学习Grep时,需要了解通配符、正则表达式两个概念,很多读者容易把彼此搞混淆,通配符主要用在Linux的Shell命令中,常用于文件或者文件名称的操作,而正则表达式用于文本内容中的字符串搜索和替换,常用在AWK、GREP、SED、VIM工具中对文本的操作。
    通配符类型详解:
*0个或者多个字符、数字;
?匹配任意一个字符;
#表示注解;
|管道符号;
;多个命令连续执行;
&后台运行指令;
!逻辑运算非;
[ ]内容范围,匹配括号中内容;
{ }命令块,多个命令匹配。

正则表达式详解:

*前一个字符匹配0次或多次;
.匹配除了换行符以外任意一个字符;
.*代表任意字符;
^匹配行首,即以某个字符开头;
$匹配行尾,即以某个字符结尾;
\(…\)标记匹配字符;
[]匹配中括号里的任意指定字符,但只匹配一个字符;
[^]匹配除中括号以外的任意一个字符;
\转义符,取消特殊含义;
\<锚定单词的开始;
\>锚定单词的结束;
{n}匹配字符出现n次;
{n,}匹配字符出现大于等于n次;
{n,m}匹配字符至少出现n次,最多出现m次;
\w匹配文字和数字字符,不匹配符号;
\W\w的反置形式,匹配一个或多个非单词字符,匹配符号;
\b单词锁定符;
\s匹配任何空白字符;
\d匹配一个数字字符,等价于[0-9]。

常用GREP工具企业演练案列:

1.统计/etc/passwd 下“root”字符总行数:

[root@localhost ~]# grep -c "root" /etc/passwd
2
或者:
[root@localhost ~]# grep -a "root" /etc/passwd|wc -l
2

2. 不区分大小写查找/etc/passwd下ROOT的所有行:

[root@localhost ~]# grep -ai "ROOT" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

3. 打印/etc/passwd下root的行号及内容:

[root@localhost ~]# grep -an "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

4. 不打印root的行:

[root@localhost ~]# grep -av "root" /etc/passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

5. 以字符test开头,接5或者3的行:

[root@localhost ~]# grep "test[3 4]" test.txt 
test3
test4
[root@localhost ~]# cat test.txt
test1
test2
test3
test4
test5
test6
test7

6. 以字符test开头,接3到7的行:

[root@localhost ~]# grep "test[3-7]" test.txt 
test3
test4
test5
test6
test7

6. 显示输出行首不是test的行:

[root@localhost ~]# grep "^[^test]" test.txt 
aaaa
bbbb
cccc
dddd
[root@localhost ~]# cat test.txt
test1
test2
test3
test4
test5
test6
test7
aaaa
bbbb
cccc
dddd

7. 匹配Root或root开头的行:

[root@localhost ~]# grep "^[Rr]oot" /etc/passwd
root:x:0:0:root:/root:/bin/bash

8. 匹配t和1之间三个字符的行:

[root@localhost ~]# grep "t...1" test.txt 
test1

9. 匹配a-z后面紧跟着oot的行:

[root@localhost ~]# grep "[a-z][o]ot" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

10. 打印字符a字符连续出现2次以上的行:

[root@localhost ~]# grep "a\{2,\}" test.txt 
aaaa

11. 打印字符a字符连续出现2次及4次的行:

[root@localhost ~]# grep "a\{2,4\}" test.txt 
aaaa

12. 打印/etc/ssh/sshd_config空行的所在的行号:

[root@localhost ~]# grep -n "^$" /etc/ssh/sshd_config 
2:
5:
7:
12:
21:
26:

13. 不匹配文件中的#和空行:

[root@localhost ~]# grep -avE "#|^$" /etc/ssh/sshd_config 
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile	.ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem	sftp	/usr/libexec/openssh/sftp-server

14. 匹配包含db或者config或者sql的文件:

[root@localhost ~]# grep   --color -ra -E    "db|config|sql"  *
anaconda-ks.cfg:# System bootloader configuration

15. 匹配IPV4地址

[root@localhost ~]# grep --color -E "([0-9]{1,3}\.){3}([0-9]{1,3})"  /etc/sysconfig/network-scripts/ifcfg-ens32 
IPADDR=192.168.2.10
NETMASK=255.255.255.0
GATEWAY=192.168.2.2
DNS1=114.114.114.114

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值