我们了解到文本处理三剑客是grep,sed.awk。今天我们就来看看第一种grep,它是一种过滤工具,只读,不会改原文。在linux系统中怎么使用grep命令的正则表达式,还有它与egrep有什么区别和联系呢?Linux附有GNU grep命令工具,它支持扩展正则表达式。grep被用于搜索定位存储在您服务器或工作站上的任何信息。

一、grep 用法:

查找文件可以用:grep      关键字      文件

                             cat         文件         通过管道符| grep  关键字

下面来看一下grep的相关参数:

--color=auto          关键字高亮显示(centos6里可以把它变成别名放在~/.bashrc里,centos7里面默认有)

-v                               关键字                           显示不包含关键字的行


grep -v "/bin/bash" /etc/passwd



 -o                              关键字                            只显示关键字本身



root@centos6 ~]#grep -o "/bin/bash" /etc/passwd
/bin/bash
/bin/bash
/bin/bash
/bin/bash



注意:如果写-o和 -v  一起,就不会显示东西了

我觉得因为是先显示匹配到的东西,然后再把里面的东西不匹配的显示,因为没有不匹配的,使用就不会显示东西了



[root@centos6 ~]#grep -vo "/bin/bash" /etc/passwd
[root@centos6 ~]#



-i                                 关键字                          忽略大小写



[root@centos6 app]#grep -i "games" /etc/passwd
GAMES
games:x:12:100:games:/usr/games:/sbin/nologin



-i   -n                          显示的结果每行前添加行号



[root@centos6 app]#grep -in "games" /etc/passwd
6:GAMES
14:games:x:12:100:games:/usr/games:/sbin/nologin



-c

-q



[root@centos6 app]#grep -q "games" /etc/passwd
[root@centos6 app]#



-A#



[root@centos6 app]#grep -A4 "gdm" /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
alice:x:500:500::/home/alice:/bin/bash



-B#



[root@centos6 app]#grep -B4 "gdm" /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologi



-C#

-e                                                                    关键字1    -e     关键字2      是或的关系



[root@centos6 app]#grep -e "gdm" -e root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin



-w



[root@centos6 app]#grep -w "gdm" /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin



-E=egrep

grep 和 egrep区别
egrep它会以扩展的正则表达式的模式来解释模式。基本的正则表达式元字符 ?、+、 {、 |、 ( 和 ) 已经失去了它们原来的意义,要使用的话用反斜线的版本 /?、/+、/{、/|、/( 和 /) 来代替。 传统的 egrep 并不支持 { 元字符,一些 egrep 的实现是以 /{ 替代的,egrep相对于grep来说省略了\

-F=fgrep                                                    不使用正则表达式

二、基本正则表达式分为4种

1.字符匹配

.                                                    匹配任意单个字符 ,类似于通配符中的?
[]:                                                  匹配指定范围内的任意单个字符
[^]:                                                匹配指定范围外的任意单个字符

[:digit:]    [:lower:]    [:upper:]     [:space:]
[:alpha:]   [:alnum:]  [:punct:]

2.匹配次数

匹配次数:用于要指定匹配其出现次数的字符的后面,用于限制其前面的字符出现的次数,默认工作于贪婪模式:

*:                                                匹配其前面的字符任意次, 0,1,多次
.*:                                               匹配任意长度的任意字符
\?:                                              匹配其前面的字符0次或者1次,
\+:                                              匹配其前面的字符1次或者多次,前面的字符至少出现1次

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

3.位置锚定

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

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

4、分组及引用

\(abc\)\{3,\}                                   abc重复3次的列出来

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

上面是四个规则,下面有一些练习题目以及结果;主要的是结合选项以及我们所写的模式来实现功能

(1)显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)



法1:
[root@centos6 ~]#cat /proc/meminfo|grep -i '^s'
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              2240 kB
Slab:             132944 kB
SReclaimable:      62904 kB
SUnreclaim:        70040 kB
法2:
[root@centos6 ~]#grep -i ^[Ss] /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              2240 kB
Slab:             132980 kB
SReclaimable:      62900 kB
SUnreclaim:        70080 kB
法3:
[root@centos6 ~]#grep -i '^S\|^s' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              2240 kB
Slab:             132960 kB
SReclaimable:      62904 kB
SUnreclaim:        70056 kB



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



[root@localhost ~]# cat /etc/grub2.cfg|egrep '^[[:space:]]+[^[:space:]]'



 (3)显示CentOS7上所有系统用户的用户名和UID

 



[root@localhost ~]# cat /etc/passwd|cut -d: -f1,3|grep '\b[0-9]\{1,3\}\b'



注意:系统用户centos7UID:1-999

(4)找出/etc/passwd用户名同shell名的行



[root@localhost ~]# cat /etc/passwd|egrep '\b^(.*)\b.*/\1$'
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:1001:1001::/home/bash:/bin/bash
nologin:x:1005:1005::/home/nologin:/sbin/nologin



 (5)利用df和grep,取出磁盘各分区利用率,并从大到小排序

 



[root@localhost ~]# df|grep -o '[0-9]\{1,3\}%' |sort -nr
100%
69%
24%
2%
1%
1%
0%
0%
0%



( 6)显示三个用户root、mage、wang的UID和默认shell



[root@localhost ~]# cat /etc/passwd |cut -d: -f1,3,7|grep -w -e 'root' -e 'mage' -e 'wang'
root:0:/bin/bash
mage:1006:/bin/bash
wang:1007:/bin/bash



 (7)找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行



[root@localhost ~]# cat /etc/rc.d/init.d/functions|egrep '^[_[[:alnum:]]+\(\)'
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {



 

(8)使用egrep取出/etc/rc.d/init.d/functions中其基名和目录名



[root@localhost ~]# echo /etc/rc.d/init.d/functions |egrep -o "[^/]+/?$"
functions
[root@localhost ~]# echo '/etc/rc.d/init.d/functions'|egrep '.*/'
/etc/rc.d/init.d/functions



(9)统计last命令中以root登录的每个主机IP地址登录次数



[root@localhost ~]# last |grep root |grep '\([0-9]\{1,3\}.\)\{3\}[0-9]\{1,3\}' -o |sort | uniq -c
 3 172.18.250.74



( 10)显示ifconfig命令结果中所有IPv4地址



[root@localhost ~]# ifconfig |egrep -o '\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
172.18.254.169
255.255.0.0
172.18.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255



 (11)将此字符串:welcome to magedu linux中的每个字符去重并排序,重复次数多的排到前面



[root@localhost ~]# echo 'welcome to magedu linux'|grep -o [^[:space:]]|sort|uniq -c|sort -nr      
       2 u
       2 o
       2 m
       2 l
       1 x
       1 w
       1 t
       1 n
       1 i
       1 g
       1 d
       1 c
       1 a