grep 的使用

grep root /etc/passwd    #单个文件查找
grep root /etc/passwd /etc/shadow /etc/gshadow    #多个文件查找
grep -l oracle /etc/passwd /etc/shadow /etc/fstab /etc/mtab    #-l 参数:在 4 个文件中查找 oracle 字符
grep -n root /etc/passwd    #查找文件匹配字符的行号
grep -v root /etc/passwd    #输出不包含字符串的行
grep ^root /etc/passwd        #^ 输出以字符串开头的行
grep bash$ /etc/passwd        #$ 输出以字符串结尾的行
grep -r root /etc/        #-r 递归查找指定字符串
grep ^$ ./.bash_profile        #^$ 查找文件中的空行,如果没有空行,将不输出任何内容
grep -i roOT /etc/passwd    #忽略查找字符串的大小写
grep -e "root" -e "oracle" /etc/passwd    #查找多个字符串

grep -f grep_pattern /etc/passwd    #用文件指定待查找的模式
创建 grep_pattern 文件,输入以下内容:
linuxtechi
root
false$

grep -c -f grep_pattern /etc/passwd    #匹配到计算的数量
grep -B 4 "root" /etc/passwd    #使用-B参数输出匹配行的前4行
grep -A 4 "root" /etc/passwd    #使用-A参数输出匹配行的后4行
grep -C 4 "root" /etc/passwd    #使用-C参数输出匹配行的前后各4行

grep -vE '^#' ./.bash_profile    #过滤前面带 # 号的行
grep -vE '^#|^$' ./.bash_profile     #过滤前面带 # 号的行,并且 $ 去掉空行

    
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值