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

SHELL命令grep练习_第1张图片
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.

SHELL命令grep练习_第2张图片

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:

SHELL命令grep练习_第3张图片

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]' 

SHELL命令grep练习_第4张图片
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

SHELL命令grep练习_第5张图片


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

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


 

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

[root@localhost ~]# egrep '.:.' /etc/passwd
SHELL命令grep练习_第6张图片
11.匹配/etc/services中开头结尾字母一样的单词

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

SHELL命令grep练习_第7张图片
 

你可能感兴趣的:(linux)