grep [参数] '字符串' 文本文件...
行
显示 行号
精 匹配
^word:以字符串word开头
word$:以字符串word结尾
[root@linuxcool ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
]# grep bin /etc/passwd #找有bin的行
]# grep bash$ /etc/passwd #找以bash结尾的行
[root@linuxcool ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
]# grep ^bin /etc/passwd #找以bin开头的行
[root@linuxcool ~]# grep linuxprobe /etc/passwd /etc/shadow
/etc/passwd:linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
/etc/shadow:linuxprobe:$6$9Av/41hCM17T2PrT$hoggWJ3J/j6IqEOSp62elhdOYPLhQ1qDho7hANcm5fQkPCQdib8KCWGdvxbRvDmqyOarKpWGxd8NAmp3j2Ln00::0:99999:7:::
[root@linuxcool ~]# grep -h linuxprobe /etc/passwd /etc/shadow
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
linuxprobe:$6$9Av/41hCM17T2PrT$hoggWJ3J/j6IqEOSp62elhdOYPLhQ1qDho7hANcm5fQkPCQdib8KCWGdvxbRvDmqyOarKpWGxd8NAmp3j2Ln00::0:99999:7:::
[root@linuxcool ~]# grep -c root /etc/passwd /etc/shadow
/etc/passwd:2
/etc/shadow:1
[root@linuxcool ~]# grep -n network anaconda-ks.cfg
17:network --bootproto=static --device=ens160 --ip=192.168.10.10 --netmask=255.255.255.0 --onboot=off --ipv6=auto --activate
18:network --hostname=www.linuxcool.com
[root@linuxcool ~]# grep -v nologin /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
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
[root@linuxcool ~]# grep -l root *
anaconda-ks.cfg
grep: Desktop: Is a directory
grep: Documents: Is a directory
grep: Downloads: Is a directory
initial-setup-ks.cfg
grep: Music: Is a directory
grep: Pictures: Is a directory
grep: Public: Is a directory
grep: Templates: Is a directory
grep: Videos: Is a directory
[root@linuxcool ~]# grep -sl root *
anaconda-ks.cfg
initial-setup-ks.cfg
[root@linuxcool ~]# grep -srl root /etc
/etc/fstab
/etc/X11/xinit/Xclients
/etc/X11/xinit/xinitrc
/etc/libreport/events.d/collect_dnf.conf
/etc/libreport/events.d/bugzilla_anaconda_event.conf
/etc/libreport/forbidden_words.conf
………………省略部分输出信息………………
[root@linuxcool ~]# grep -x cd anaconda-ks.cfg
[root@linuxcool ~]# grep -x cdrom anaconda-ks.cfg
cdrom
[root@linuxcool ~]# grep -q linuxprobe anaconda-ks.cfg | echo $?
0
[root@linuxcool ~]# grep -q linuxcool anaconda-ks.cfg | echo $?
1
[root@linuxcool ~]# grep -q '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO"
YES
[root@linuxcool ~]# grep -c ^$ anaconda-ks.cfg
6
[root@linuxcool ~]# grep -n ^$ user.txt
[root@linuxcool ~]# grep -v ^$ /etc/default/useradd
[root@linuxcool ~]# grep -v ^# /etc/default/useradd #在Linux系统中,大多数配置文件以#开头的行,为注释行
[root@linuxcool ~]# grep -v ^# /etc/default/useradd | grep -v ^$ #显示有效配置(去除空行与注释行)
[root@linuxcool ~]# grep -v ^# /etc/default/useradd | grep -v ^$ > /opt/nsd03.txt #显示不要空行和#号的行
[root@linuxcool ~]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/log.txt #显示不要空行和#号的行
[root@linuxcool ~]# grep -i man /etc/man_db.conf #忽略大小写
[root@linuxcool ~]# grep "[root]" user.txt #找root四个字符任意一个
[root@linuxcool ~]# grep "[a-z]" user.txt #找所有小写字母
[root@linuxcool ~]# grep "[A-Z]" user.txt #找所有大写字母
[root@linuxcool ~]# grep "[0-9]" user.txt #找所有数字
[root@linuxcool ~]# grep "[^a-Z]" user.txt #找字母之外的内容,^写在[]里是取反效果
[root@linuxcool ~]# grep "r..t" user #找rt之间有2个任意字符的行
[root@linuxcool ~]# grep "ro*t" user #找rt,中间的o有没有都行
[root@linuxcool ~]# grep "." user #找任意单个字符
[root@linuxcool ~]# grep ".*" user #找任意
[root@linuxcool ~]# grep "ro\{2,4\}t" user #找rt中间的o可以是2~4个
[root@linuxcool ~]# grep "ro\{3,\}t" user #找rt中间的o可以是3个以及3个以上
[root@linuxcool ~]# grep "ro\{2\}t" user #找rt中间的o必须是2个