grep命令参数
grep ‘word’ 文件名
grep ‘word’ 文件1 文件2 文件3 …文件N
grep ‘字符串1 字符串2’ 文件名
cat 文件名| grep ”字符串“
命令 | grep “字符串”
grep –color “字符串” 文件名
grep ‘word’ 文件1 文件2 文件3 …文件N
grep ‘字符串1 字符串2’ 文件名
cat 文件名| grep ”字符串“
命令 | grep “字符串”
grep –color “字符串” 文件名
如何使用grep命令去搜索某个文件中的内容呢?
查找 /etc/passwd 文件中是否存在user1这个用户,可以使用下面的命令:
[root@devops ~]# grep user1 /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
[root@devops ~]#
user1:x:501:501::/home/user1:/bin/bash
[root@devops ~]#
如果想忽略大小写去查找比如:User1,USER1或者其它的组合,可以使用-i选项
[root@devops ~]# grep -i user1 /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash
[root@devops ~]#
user1:x:501:501::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash
[root@devops ~]#
grep 命令递归查找
你可以递归去查找某个目录下各级子目录里的匹配查找模式的行,使用-r 选项
[root@devops ~]# grep -r user1 /etc | more
/etc/group-:user1:x:501:
/etc/shadow:user1:!!:16176:0:99999:7:::
/etc/group:user1:x:501:
/etc/passwd-:user1:x:501:501::/home/user1:/bin/bash
/etc/gshadow-:user1:!::
/etc/shadow-:user1:!!:16176:0:99999:7:::
/etc/passwd:user1:x:501:501::/home/user1:/bin/bash
/etc/gshadow:user1:!::
/etc/postfix/virtual:# user1@virtual-alias.domain address1
[root@devops ~]#
/etc/group-:user1:x:501:
/etc/shadow:user1:!!:16176:0:99999:7:::
/etc/group:user1:x:501:
/etc/passwd-:user1:x:501:501::/home/user1:/bin/bash
/etc/gshadow-:user1:!::
/etc/shadow-:user1:!!:16176:0:99999:7:::
/etc/passwd:user1:x:501:501::/home/user1:/bin/bash
/etc/gshadow:user1:!::
/etc/postfix/virtual:# user1@virtual-alias.domain address1
[root@devops ~]#
使用grep去匹配某一个单词
选项-w使grep命令只查找作为一个词,而不是词的一部分去做匹配
举例说明:
[root@devops ~]# grep user1 /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
user1add:x:503:503::/home/user1add:/bin/bash
[root@devops ~]# grep -w ‘user1’ /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
user1:x:501:501::/home/user1:/bin/bash
user1add:x:503:503::/home/user1add:/bin/bash
[root@devops ~]# grep -w ‘user1’ /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
在本例中能够匹配“user1″的模式有两行,那么当你只想匹配一个完整的user1这个单词的时候,可以使用-w选项。
使用grep命令去查找2个不同的词
[root@devops ~]# egrep -w “user1|USER1” /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash
[root@devops ~]#
user1:x:501:501::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash
[root@devops ~]#
统计grep查找出来的行数
grep命令可以使用-c选项来显示出匹配到的行数
[root@devops ~]# grep -c ‘user1’ /etc/passwd
2
2
使用-n 选项可以显示出查找到的行在文件中的行号
[root@devops ~]# grep -n ‘user1’ /etc/passwd
23:user1:x:501:501::/home/user1:/bin/bash
25:user1add:x:503:503::/home/user1add:/bin/bash
23:user1:x:501:501::/home/user1:/bin/bash
25:user1add:x:503:503::/home/user1add:/bin/bash
grep反向查找只显示不匹配的行
你可以使用-v 选项来显示不匹配的行,例如:
[root@devops ~]# grep -v user1 /etc/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
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
root:x:0:0:root:/root:/bin/bash
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
linux 管道符和grep命令的搭配使用
grep命令经常会和linux或unix管道符搭配使用,如显示硬盘设备的名字:
[root@devops ~]# dmesg | egrep ‘(s|h)d[a-z]’
NMI watchdog disabled (cpu0): hardware events not enabled
sd 2:0:0:0: [sda] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
sd 2:0:0:0: [sda] Write Protect is off
sd 2:0:0:0: [sda] Mode Sense: 61 00 00 00
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sda: sda1 sda2
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sd 2:0:0:0: [sda] Attached SCSI disk
dracut: Scanning devices sda2 for LVM logical volumes vg_devops/lv_root vg_devops/lv_swap
EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts:
SELinux: initialized (dev sda1, type ext4), uses xattr
NMI watchdog disabled (cpu0): hardware events not enabled
sd 2:0:0:0: [sda] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
sd 2:0:0:0: [sda] Write Protect is off
sd 2:0:0:0: [sda] Mode Sense: 61 00 00 00
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sda: sda1 sda2
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sd 2:0:0:0: [sda] Attached SCSI disk
dracut: Scanning devices sda2 for LVM logical volumes vg_devops/lv_root vg_devops/lv_swap
EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts:
SELinux: initialized (dev sda1, type ext4), uses xattr
显示cpu模块名字:
[root@devops ~]# cat /proc/cpuinfo | grep -i ‘Model’
model : 42
model name : Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
[root@devops ~]#
model : 42
model name : Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
[root@devops ~]#
如何只输出包含模式的文件名,而不是输出文本行
[root@devops ~]# grep -l ‘user1’ /etc/*
/etc/group
/etc/group-
/etc/gshadow
/etc/gshadow-
/etc/passwd
/etc/passwd-
/etc/shadow
/etc/shadow-
/etc/group
/etc/group-
/etc/gshadow
/etc/gshadow-
/etc/passwd
/etc/passwd-
/etc/shadow
/etc/shadow-
转载请注明:爱开源 » grep:如何在linux下使用grep命令