grep命令详解

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值