SHELL命令grep练习

1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行:

[root@localhost ~]# egrep -i '^h' /etc/rc.d/rc.sysinit

2、显示/etc/passwd中以sh结尾的行;

[root@localhost ~]# grep -E 'sh$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
fox:x:1000:1000::/home/fox:/bin/bash
nfs-upload:x:210:210::/home/nfs-upload:/bin/bash
tom:x:1001:1001::/home/tom:/bin/bash


3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行:

[root@localhost ~]# grep -E '^#[[:space:]]+[^[:space:]]' /etc/fstab
# /etc/fstab
# Created by anaconda on Sun Nov  5 12:23:58 2023
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.

4、查找/etc/rc.d/rc.oca1中包含“以to开始并以to结尾”的字串行;

[root@localhost ~]# egrep '\b(to).*\1\b' /etc/rc.d/rc.local
# In contrast to previous versions due to parallel execution during boot



5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;

[root@localhost ~]# grep -w 's.*d' /etc/inittab
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:



6、查找ifconfig命令结果中的1-255之间的整数;

[root@localhost ~]# ifconfig | egrep -o '[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5]' 


7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行

[root@localhost ~]# grep -i 'Failed'  /var/log/secure

8、在/etc/passwd中取出默认she11为bash

[root@localhost ~]#  grep -w 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
fox:x:1000:1000::/home/fox:/bin/bash
nfs-upload:x:210:210::/home/nfs-upload:/bin/bash
tom:x:1001:1001::/home/tom:/bin/bash


9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息

[root@localhost ~]# ls /etc | grep '\bns.*.conf\b' 
nsswitch.conf
nsswitch.conf.bak


 

10、高亮显示passwd文件中冒号,及其两侧的字符

[root@localhost ~]# egrep '.:.' /etc/passwd

11.匹配/etc/services中开头结尾字母一样的单词

[root@localhost ~]# egrep  '^(\b[a-zA-Z]+\b).*\1$' /etc/services


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值