grep命令主要用来查看文本文件,但是grep命令不能单独用来直接查看文件,需要配合相应字符串或选项加字符串
grep [字符串] 文件路径
^字符串 表示以该字符串开头的行
字符串$ 表示以该字符串结尾的行
[root@server0 ~]# grep root /etc/passwd #查看含有root字符的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin #查看含有bash字符的行
[root@server0 ~]# grep bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
alex:x:3456:3456::/home/alex:/bin/bash
natasha:x:3457:3458::/home/natasha:/bin/bash
harry:x:3458:3459::/home/harry:/bin/bash
lily:x:3460:3462::/home/lily:/bin/bash
[root@server0 ~]# grep ^root /etc/passwd #查看以root字符开头的行
root:x:0:0:root:/root:/bin/bash
[root@server0 ~]# grep bash$ /etc/passwd #查看以bash字符结尾的行
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
alex:x:3456:3456::/home/alex:/bin/bash
natasha:x:3457:3458::/home/natasha:/bin/bash
harry:x:3458:3459::/home/harry:/bin/bash
lily:x:3460:3462::/home/lily:/bin/bash
zhangsan:x:3461:3463::/home/zhangsan:/bin/bash
显示/etc/login.defs配置文件有效信息(去除空行 去除注释行)
注:^# 表示 以井号开头的行 ^#表示空行
[root@server0 ~]# grep -v ^# /etc/login.defs | grep -v ^$
MAIL_DIR /var/spool/mail
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
UID_MIN 1000
UID_MAX 60000
SYS_UID_MIN 201
SYS_UID_MAX 999
GID_MIN 1000
GID_MAX 60000
SYS_GID_MIN 201
SYS_GID_MAX 999
CREATE_HOME yes
UMASK 077
USERGROUPS_ENAB yes
ENCRYPT_METHOD SHA512
grep [-选项 ] [参数] 文件路径
-v 表示反向匹配
[root@server0 ~]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
...
......
lily:x:3460:3462::/home/lily:/bin/bash
zhangsan:x:3461:3463::/home/zhangsan:/bin/bash
-i 忽略大小写
[root@server0 ~]# grep -i ROOT /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin