grep简介
global regular expression print (全局正则表达式打印)源于最早的行编辑器ed
grep [option] pattern [file]
在file中查找与pattern匹配的内容
如果找到按整行输出
-i 忽略大小写
-n 输出该行的的同时,也输出该行的行号
-s 在没找到匹配的内容时,不显示错误信息
-l 从多个文件件中查找,只输出匹配内容的文件名
-h 从多个文件中查找时,只输出匹配的内容,不显示文件名称
-c 只输出匹配内容的总行数
-v 反转查找,即输出匹配内容以外的行
gerp工作原理
grep工作时,总是以行为单位进行查找。首先将文本的第一行读入缓冲区并进行查找,
找到匹配字符串,则输出整行。否则清空缓冲区,读入下一个文本行继续查找直到文本结束
普通实例
(1)查找关键字
[
root@localhost ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
(2)显示匹配到的行,并且显示其所在的行号
[
root@localhost ~]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
(3)统计匹配到的行的总行数
(4)不区分大小写匹配字符串,从而输出其匹配到的行
[
root@localhost ~]# grep -i "changerjjlee" /etc/passwd
CHANGERJJLEE:x:501:501::/home/CHANGERJJLEE:/bin/bash
changerjjlee:x:502:502::/home/changerjjlee:/bin/bash
[
root@localhost ~]# grep "changerjjlee" /etc/passwd
changerjjlee:x:502:502::/home/changerjjlee:/bin/bash
(5)反转查找
[
root@localhost ~]# grep -v "sbin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
serer1:x:500:500:server1:/home/serer1:/bin/bash
CHANGERJJLEE:x:501:501::/home/CHANGERJJLEE:/bin/bash
changerjjlee:x:502:502::/home/changerjjlee:/bin/bash
[
root@localhost ~]# grep -vi "changerjjlee" /etc/passwd |grep -vi "sbin"
root:x:0:0:root:/root:/bin/bash
serer1:x:500:500:server1:/home/serer1:/bin/bash
###输出不含changerjjlee CHANGERJJLEE sbin 的行###
(6)多文件查找仅输出文件名
[
root@localhost ~]# grep -l "sbin" /etc/*
/etc/anacrontab
/etc/crontab
/etc/csh.login
/etc/dnsmasq.conf
/etc/kdump.conf
/etc/login.defs
/etc/man.config
/etc/passwd
/etc/passwd-
/etc/passwd.OLD
/etc/prelink.conf
/etc/profile
/etc/rc
/etc/rc.sysinit
/etc/readahead.conf
/etc/sestatus.conf
/etc/shells
/etc/sudoers
/etc/warnquota.conf
(7)多文件查找输出文件内容,不显示文件名
[
root@localhost ~]# grep -h "root" /etc/passwd /etc/shadow
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:$1$Vdr/Zm0G$eWEpQxy37ejAN1V88D.sO.:16889:0:99999:7:::
( 8 )在输出中查找
[
root@localhost ~]# cat /etc/passwd | grep "root"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
(9)匹配变量查找
[
root@localhost ~]# A="root" ;cat /etc/passwd | grep "$A"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[
root@localhost ~]# A='root' ;cat /etc/passwd | grep "$A"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
配合正则表达式的实例
(10)行首匹配
[
root@localhost ~]# grep '^s' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
serer1:x:500:500:server1:/home/serer1:/bin/bash
[
root@localhost ~]# grep '^sa' /etc/passwd
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
######第四个字符为 d 的行匹配######
[
root@localhost ~]# grep '^...d' /etc/passwd
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
(11)行尾匹配
[
root@localhost ~]# grep -v '[i][n]$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
serer1:x:500:500:server1:/home/serer1:/bin/bash
CHANGERJJLEE:x:501:501::/home/CHANGERJJLEE:/bin/bash
changerjjlee:x:502:502::/home/changerjjlee:/bin/bash
(12)范围模式的匹配
[
root@localhost ~]# grep ':4[5-9]' /etc/passwd
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
[
root@localhost ~]# grep ':4[5-9]:' /etc/passwd
apache:x:48:48:Apache:/var/www:/sbin/nologin
[
root@localhost ~]# grep ':4[5-9][1-9]' /etc/passwd
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
(13)次数匹配模式
###### w 出现一到三次 ######
[
root@localhost ~]# grep 'w\{1,3\}' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
###### w 出现俩次以上 ######
[
root@localhost ~]# grep 'w\{2,\}' /etc/passwd
apache:x:48:48:Apache:/var/www:/sbin/nologin
###### w出现一次 ######
[
root@localhost ~]# grep 'w\{1\}' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
###### w出现一次 并且以n结尾 ######
[
root@localhost ~]# grep 'w\{1\}n' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
###### wn 结尾的行 ######
###### 统计空行的总行数######
###### 以空行开头的行######
(15)或匹配模式
[
root@localhost Desktop]# grep -E 'home|root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
serer1:x:500:500:server1:/home/serer1:/bin/bash
CHANGERJJLEE:x:501:501::/home/CHANGERJJLEE:/bin/bash
changerjjlee:x:502:502::/home/changerjjlee:/bin/bash
(16)与匹配模式
[
root@localhost Desktop]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[
root@localhost Desktop]# grep "root" /etc/passwd |grep "sbin"
operator:x:11:0:operator:/root:/sbin/nologin
grep工具应用实例
###### 精简配置文件######
以精简samba服务的配置文件smb.conf为例
cd /etc/samba/
mv smb.conf smb.conf_backup
cat smb.conf_backup | grep -v '#' | grep -v '^;' | grep -v '^$' > smb.conf
cat smb.conf
###### 从系统管理命的输出中查找 ######
查看蓝牙服务在不同运行级别的启动状态
chkconfig --list | grep bluetooth