grep与egrep

grep与egrep

2018年06月21日 22:42:30 Nick_9705 阅读数:6731git

我的以为egrep比较好用,感受改良了grep的一些不能够直接操做的东西,可是整体来讲仍是没太大区别的,都是一个过滤工具。正则表达式

grep 和 egrep 都要经过 正则表达式来筛选咱们想要的东西,只能筛选文本内容,不能对目录筛选,若是想筛选目录能够经过管道把目录传过去,或者把目录存到文件里头再筛选centos

 

–color=auto:对匹配到的文本着色后高亮显示bash

这个是grep的别名tcp

alias egrep=’egrep –color=auto’
alias fgrep=’fgrep –color=auto’
alias grep=’grep –color=auto’工具

因此使用的时候默认带高亮参数spa

若是不想高亮能够 –color=none.net

 

-o 是只显示匹配到的字符get

[root@localhost tmp]# grep -o “root” /tmp/passwd
root
root
root
rootit

 

-i是忽略大小写

[root@localhost tmp]# grep -io “h” /tmp/h1
H
H
h
h
H
h

 

-v  是忽略匹配到的行

[root@localhost tmp]# grep -v “h” /tmp/h1

HH

 

若是写-o和 -v  一块儿

就不会显示东西了

我以为由于是先显示匹配到的东西,而后再把里面的东西不匹配的显示,由于没有不匹配的,使用就不会显示东西了

 

-E 容许使用扩展正则表达式

就是egrep

 

-q  是静默模式

就是说明都不输出

[root@localhost tmp]# grep -q “root” /tmp/passwd
[root@localhost tmp]#

 

-A #:after 后#行

[root@localhost tmp]# grep -A2 “root” /tmp/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

后2行一块儿输出

-B #:before 前#行

[root@localhost tmp]# grep -B2 “root” /tmp/passwd
root:x:0:0:root:/root:/bin/bash

halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

没有的话,就把–

-C #:先后各#行

[root@localhost tmp]# grep -C2 “root” /tmp/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

 

和-A #  -B# 同样

[root@localhost tmp]# grep -A2 -B2 “root” /tmp/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

基本正则表达式元字符
分为四种
一、字符匹配
.:匹配任意单个字符                  相似于通配符中的?
[]:匹配指定范围内的任意单个字符
[^]:匹配指定范围外的任意单个字符
[:digit:],[:lower:],[:upper:],[:space:]
[:alpha:],[:alnum:],[:punct:]

 

 

二、匹配次数:用于要指定匹配其出现次数的字符的后面,用于限制其前面的字符出现的次数,默认工做于贪婪模式:
*:匹配其前面的字符任意次, 0,1,屡次
.*:匹配任意长度的任意字符
\?:匹配其前面的字符0次或者1次,
\+:匹配其前面的字符1次或者屡次,前面的字符至少出现1次

\{m\}:匹配其前面的字符m次
\{m,n\}:匹配前面的字符至少m次至多n次
\{0,n\}:至多匹配n次
\{m,\}:至少匹配m次

三、位置锚定
^:行首锚定:用于模式的最左侧
$:行尾锚定:用于模式的最右侧
^patten$:用patten来匹配整行
^$:空白行
^[[:space:]]*$:

\<或者\b:词首锚定,用于单词模式的左侧
\>或者\b:词尾锚定,用于单子的右侧
\<patten\>:匹配完整的单词

四、分组及引用

:将一个或者多个字符捆绑在一块儿,看成一个总体进行处理

分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,
\1:模式从左侧起,第一个左括号以及与之匹配的右括号之间的模式匹配到的字符
\2:模式从左侧起,第二个左括号以及与之匹配的右括号之间的模式匹配到的字符

 

上面是四个规则,下面有我本身的从网上找的一些练习题目以及结果;

 

主要的是结合选项以及咱们所写的模式来实现功能

 

好比说

一、显示/proc/meminfo文件中以大小s开头的行

 

[root@localhost ~]# grep -i “^s” /proc/meminfo

 

二、显示/etc/passwd文件中不以/bin/bash结尾的行

 

[root@localhost ~]# grep -v “/bin/bash$” /etc/passwd

三、找出/etc/passwd中的两位或三位数

 

[root@localhost ~]# grep “\b[0-9]\{2,3\}\b” /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

 

四、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白 字符开头的且后面存非空白字符的行

 

[root@localhost ~]# grep “^[[:space:]]\+[^[:space:]]” /etc/grub2.cfg
load_env

 

五、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多 个空白字符结尾的行

[root@localhost ~]# netstat -tan | grep “LISTEN[[:space:]]\+”
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN

六、以root开头和结尾的行

[root@localhost ~]# cat /tmp/test

rootadfafroot
raatadfasadfraat
raotasdfaroat
rootadfafraat

 

而后咱们使用分组那个规则

[root@localhost ~]# grep “r..tr..t.*\1” /tmp/test
rootadfafroot
raatadfasadfraat

利用引用来查看etc下以se结尾的文件

[root@localhost ~]# cat $(find /etc/ -type f | grep “se$”)
CentOS Linux release 7.4.1708 (Core)
NAME=”CentOS Linux”
VERSION=”7 (Core)”
ID=”centos”
ID_LIKE=”rhel fedora”
VERSION_ID=”7″
PRETTY_NAME=”CentOS Linux 7 (Core)”
ANSI_COLOR=”0;31″
CPE_NAME=”cpe:/o:centos:centos:7″
HOME_URL=”https://www.centos.org/”
BUG_REPORT_URL=”https://bugs.centos.org/”

CENTOS_MANTISBT_PROJECT=”CentOS-7″
CENTOS_MANTISBT_PROJECT_VERSION=”7″
REDHAT_SUPPORT_PRODUCT=”centos”
REDHAT_SUPPORT_PRODUCT_VERSION=”7″

 

做业:使用echo输入一个绝对路径,使用egrep取其路径名

[root@localhost ~]# echo “/etc/profile.d/lang.sh” | grep -o “^/.*/”
/etc/profile.d/

 

egrep 和grep -E同样

egrep 和 grep的功能几乎同样,可是使用的是拓展的正则表达式

拓展正则表达式没有.*了,而后就是少了使用\

好比说

?:0次或者1次                                    在grep里头要写\?
+:其前面的字符至少出现1次                                         \+
{m}:其前的字符出现m次                                                  \{m\}

而后加入了或的一个逻辑

a|b:a或者b C|cat:C或者cat (C|c)at:cat或者Cat

相关文章
相关标签/搜索