Rocky基础练习题---grep练习题
作者:快盘下载 人气:1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
[root@rocky8 ~]# cat /proc/meminfo |egrep -io '^[s].*'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 8860 kB
Slab: 95680 kB
SReclaimable: 44736 kB
SUnreclaim: 50944 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@rocky8 ~]# cat /proc/meminfo |egrep -v '^[^s|S].*'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 8860 kB
Slab: 95676 kB
SReclaimable: 44736 kB
SUnreclaim: 50940 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@rocky8 ~]# cat /proc/meminfo |egrep -o '^[sS].*'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 8860 kB
Slab: 95676 kB
SReclaimable: 44736 kB
SUnreclaim: 50940 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@rocky8 ~]# egrep -io '^[s].*' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 8860 kB
Slab: 95680 kB
SReclaimable: 44736 kB
SUnreclaim: 50944 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@rocky8 ~]# cat /proc/meminfo |sed -rn '/^[sS].*/p'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 8860 kB
Slab: 95684 kB
SReclaimable: 44736 kB
SUnreclaim: 50948 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
2、显示/etc/passwd文件中不以/bin/bash结尾的行
[root@rocky8 ~]# cat /etc/passwd |egrep -v '/bin/bash$'
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
....
3、显示用户rpc默认的shell程序
[root@rocky8-1 ~]#cat /etc/passwd |egrep '^rpc:'|cut -d: -f7
/sbin/nologin
[root@rocky8 ~]# cat /etc/passwd |egrep adm|egrep -o '[^0-9:]+$'
/sbin/nologin
4、找出/etc/passwd中的两位或三位数
[root@rocky8 ~]# cat /etc/passwd |egrep -o '<[0-9]{2,3}>'
5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
[root@rocky8 ~]# cat /etc/grub2.cfg |egrep '^[[:space:]]+[^[:space:]]+'
6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行
[root@rocky8 ~]# netstat -tan |egrep 'LISTEN[[:space:]]+$'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
7、显示CentOS7上所有UID小于1000以内的用户名和UID
[root@rocky8 ~]# cat /etc/passwd |egrep '<x:[0-9]{1,3}>' |cut -d: -f1,3
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
...
8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行
[root@rocky8 ~]# for i in bash testbash basher sh nologin;do useradd -s /sbin/nologin $i;done
[root@rocky8 ~]# tail -5 /etc/passwd
bash:x:1001:1001::/home/bash:/sbin/nologin
testbash:x:1002:1002::/home/testbash:/sbin/nologin
basher:x:1003:1003::/home/basher:/sbin/nologin
sh:x:1004:1004::/home/sh:/sbin/nologin
nologin:x:1005:1005::/home/nologin:/sbin/nologin
[root@rocky8 ~]# cat /etc/passwd |egrep '^(.*>).*<1$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1005:1005::/home/nologin:/sbin/nologin
9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
[root@rocky8 ~]# df |egrep '/dev/sda'|egrep -o '[0-9]{1,3}%'|sort -rn
20%
7%
10、显示三个用户root、mage、wang的UID和默认shell
[root@rocky8 ~]# cat /etc/passwd | egrep '^(root|mage|wang)>'|cut -d: -f3,7
0:/bin/bash
11、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
[root@rocky8 ~]# cat /etc/init.d/functions |egrep '^<[a-Z_]+>()'
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
12、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@rocky8 ~]# echo /etc/rc.d/init.d/functions | egrep -o '[^/]+/?$'
functions
13、使用egrep取出上面路径的目录名
[root@rocky8 ~]# echo /etc/rc.d/init.d/functions | egrep -o '.*/' |egrep -o '.*[^/]'
/etc/rc.d/init.d
14、统计last命令中以root登录的每个主机IP地址登录次数
[root@rocky8-1 ~]#last|egrep 'root'|egrep -o '([0-9]{1,3}.){3}[0-9]{1,3}'|sort|uniq -c
42 10.0.0.1
15、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
[root@rocky8 ~]# seq 255 |egrep -o "<[0-9]>"
1
2
3
4
5
6
7
8
9
[root@rocky8 ~]# seq 255 |egrep -o "<[0-9]{2}>"
10
11
12
13
...
[root@rocky8 ~]# seq 255 |egrep -o "<[1][0-9]{2}>"
100
101
102
...
[root@rocky8 ~]# seq 255 |egrep -o "<[2][0-4][0-9]>"
200
201
202
203
204
205
...
[root@rocky8 ~]# seq 255 |egrep -o "<[2][5][0-5]>"
250
251
252
253
254
255
16、显示ifconfifig命令结果中所有IPv4地址
[root@rocky8 ~]# ifconfig|egrep -o '([0-9]{1,3}.){3}[0-9]{1,3}'|egrep -v -e '^255' -e'255$'
10.0.0.151
127.0.0.1
[root@rocky8 ~]# ifconfig|egrep -o '([0-9]{1,3}.){3}[0-9]{1,3}'|egrep -v '^255|255$'
10.0.0.151
127.0.0.1
17、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
[root@rocky8 ~]# echo welcome to magedu linux|egrep -o "[^ ]" |sort|uniq -c|sort -rn
3 e
2 u
2 o
2 m
2 l
1 x
1 w
1 t
1 n
1 i
1 g
1 d
1 c
1 a
18、找出ifconfifig “网卡名” 命令结果中本机的IPv4地址
[root@rocky8 ~]# ifconfig ens160 |egrep -o '([0-9]{1,3}.){3}[0-9]{1,3}'|egrep -v "^255|255$"
10.0.0.151
19、查出分区空间使用率的最大百分比值
[root@rocky8 ~]# df|egrep "/dev/sd"|egrep -o "[0-9]{1,3}%"|sort -rn|head -1
20%
20、查出用户UID最大值的用户名、UID及shell类型
[root@rocky8 ~]# cat /etc/passwd |cut -d: -f1,3,7|sort -rn -t: -k2|head -1
nobody:65534:/sbin/nologin
21、查出/tmp的权限,以数字方式显示
[root@rocky8 ~]# stat /tmp |egrep Uid|cut -d/ -f1|cut -d( -f2
1777
[root@rocky8 ~]# stat /tmp|egrep Uid|cut -d/ -f1|tr -dc '[0-9 ]+'
1777
22、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@rocky8 ~]# ss -nt |egrep '^ESTAB' |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -rn
2 10.0.0.1
加载全部内容