课8正则练习

发布时间 2023-12-18 16:53:36作者: 胖虎小夫

1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

[root@srehost ~]#cat /proc/meminfo |egrep -o "^(s|S).*"
SwapCached: 0 kB
SwapTotal: 8257532 kB
SwapFree: 8257532 kB
Shmem: 11044 kB
Slab: 128976 kB
SReclaimable: 66212 kB
SUnreclaim: 62764 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@srehost ~]#
[root@srehost ~]#
[root@srehost ~]#
[root@srehost ~]#cat /proc/meminfo |egrep -i "^s.*"
SwapCached: 0 kB
SwapTotal: 8257532 kB
SwapFree: 8257532 kB
Shmem: 11044 kB
Slab: 128988 kB
SReclaimable: 66212 kB
SUnreclaim: 62776 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB

 


2、显示/etc/passwd文件中不以/bin/bash结尾的行

[root@srehost ~]#cat /etc/passwd | egrep ".*/bin/bash$"
root:x:0:0:root:/root:/bin/bash
jin:x:1000:1000:jin:/home/jin:/bin/bash
mageia:x:1100:1100::/home/linux/:/bin/bash
user1:x:2003:2003::/home/user1:/bin/bash
user2:x:2004:2004::/home/user2:/bin/bash
user3:x:2005:2005::/home/user3:/bin/bash

[root@srehost ~]#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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pipewire:x:996:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:995:989:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:994:988:GlusterFS daemons:/run/gluster:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
chrony:x:993:987::/var/lib/chrony:/sbin/nologin
saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:991:985:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sssd:x:982:982:User for sssd:/:/sbin/nologin
cockpit-ws:x:981:981:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:980:980:User for cockpit-ws instances:/nonexisting:/sbin/nologin
colord:x:979:979:User for colord:/var/lib/colord:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
setroubleshoot:x:978:978::/var/lib/setroubleshoot:/sbin/nologin
flatpak:x:977:977:User for flatpak system helper:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
clevis:x:976:976:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
gnome-initial-setup:x:975:975::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
radiusd:x:95:95:radiusd user:/var/lib/radiusd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
slackware:x:2002:2019::/home/slackware:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin


3、显示用户rpc默认的shell程序


[root@srehost ~]#cat /etc/passwd |grep rpc
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[root@srehost ~]#cat /etc/passwd |egrep "^rpc:.*"
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
[root@srehost ~]#cat /etc/passwd |egrep "^rpc:.*" | cut -d ':' -f7
/sbin/nologin

4、找出/etc/passwd中的两位或三位数

[root@srehost ~]#cat /etc/passwd |egrep -n -o "[0-9]{2,3}"
9:12
10:11
11:12
11:100
12:14
12:50
13:655
13:34
13:655
13:34
14:81
14:81
15:999
15:997
16:193
16:193
17:59
17:59
18:998
18:996
19:997
19:995
20:172
20:172
21:996
21:992
22:171
22:171
23:107
23:107
24:113
24:113
25:995
25:989
26:994
26:988
27:32
27:32
28:70
28:70
29:993
29:987
30:992
30:76
31:991
31:985
32:983
32:983
33:75
33:75
34:982
34:982
35:981
35:981
36:980
36:980
37:979
37:979
38:29
38:29
39:978
39:978
40:977
40:977
41:42
41:42
42:976
42:976
43:975
43:975
44:72
44:72
45:74
45:74
46:100
46:100
47:95
47:95
48:89
48:89
49:110
49:110
50:200
50:201
51:200
51:200
52:200
52:200
53:200
53:200
54:48
54:48

 

5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行


[root@srehost jin]#cat /etc/grub2.cfg |egrep -n "^[ \f\n\r\t\v][^ \f\n\r\t\v]+"
15:fi
23:fi
29:fi
39:fi
41:function savedefault {
48:function load_video {
62:terminal_output console
70:fi
86:fi
103:fi
114:fi
122:fi
141:fi
153:fi
160:fi
185:fi
211:fi


6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

[root@srehost jin]#netstat -tan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 248 192.168.81.11:22 192.168.81.1:5010 ESTABLISHED
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
[root@srehost jin]#
[root@srehost jin]#
[root@srehost jin]#netstat -tan| egrep ".*LISTEN[ /f/n/r/t/v]$"
[root@srehost jin]#netstat -tan| egrep ".*LISTEN[ /f/n/r/t/v]+$"
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
[root@srehost jin]#netstat -tan| egrep ".*LISTEN.*[ /f/n/r/t/v]$"
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN


7、显示CentOS7上所有UID小于1000以内的用户名和UID

[root@srehost jin]#cat /etc/passwd | egrep ".*:[0-9]{1,3}:.*"
root:x:0:0:root:/root:/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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pipewire:x:996:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:995:989:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:994:988:GlusterFS daemons:/run/gluster:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
chrony:x:993:987::/var/lib/chrony:/sbin/nologin
saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:991:985:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dnsmasq:x:983:983:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sssd:x:982:982:User for sssd:/:/sbin/nologin
cockpit-ws:x:981:981:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:980:980:User for cockpit-ws instances:/nonexisting:/sbin/nologin
colord:x:979:979:User for colord:/var/lib/colord:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
setroubleshoot:x:978:978::/var/lib/setroubleshoot:/sbin/nologin
flatpak:x:977:977:User for flatpak system helper:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
clevis:x:976:976:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
gnome-initial-setup:x:975:975::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
radiusd:x:95:95:radiusd user:/var/lib/radiusd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@srehost jin]#cat /etc/passwd | egrep ".*:[0-9]{1,3}:.*"| cut -d ":" -f 1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
dbus
systemd-coredump
systemd-resolve
tss
polkitd
geoclue
rtkit
pipewire
pulse
qemu
usbmuxd
unbound
gluster
rpc
avahi
chrony
saslauth
libstoragemgmt
dnsmasq
radvd
sssd
cockpit-ws
cockpit-wsinstance
colord
rpcuser
setroubleshoot
flatpak
gdm
clevis
gnome-initial-setup
tcpdump
sshd
radiusd
postfix
apache


8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户
名和shell同名的行

[root@srehost jin]#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
bash:x:2006:2006::/home/bash:/bin/bash
nologin:x:2010:2010::/home/nologin:/sbin/nologin


9、利用df和grep,取出磁盘各分区利用率,并从大到小排序

[root@srehost jin]#df -h | egrep "[0-9]{1,3}%" | sort -k5 -nr
/dev/loop0 11G 11G 0 100% /mnt/cdrom
/dev/mapper/cl-home 24G 11G 14G 44% /home
/dev/sda1 1014M 259M 756M 26% /boot
/dev/mapper/cl-root 48G 4.9G 43G 11% /
tmpfs 775M 12K 775M 1% /run/user/42
tmpfs 3.8G 9.6M 3.8G 1% /run
tmpfs 775M 0 775M 0% /run/user/0
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
tmpfs 3.8G 0 3.8G 0% /dev/shm
devtmpfs 3.8G 0 3.8G 0% /dev

 

##################################################################################################################################

 

扩展正则表达式练习


1、显示三个用户root、mage、wang的UID和默认shell

[root@srehost ~]#cat /etc/passwd |egrep "^(root|jin|apache)" | cut -d : -f 1,7
root:/bin/bash
jin:/bin/bash
apache:/sbin/nologin
[root@srehost ~]#


2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@srehost ~]#cat /etc/rc.d/init.d/functions | egrep -o -n "^[_[:alnum:]].*\(.*\).*"
25:systemctl_redirect () {
100:checkpid() {
109:__kill_pids_term_kill_checkpids() {
135:__kill_pids_term_kill() {
184:__pids_var_run() {
220:__pids_pidof() {
227:daemon() {
321:killproc() {
404:pidfileofproc() {
419:pidofproc() {
445:status() {
521:echo_success() {
532:echo_failure() {
543:echo_passed() {
554:echo_warning() {
566:update_boot_stage() {
574:success() {
580:failure() {
588:passed() {
595:warning() {
602:action() {
615:strstr() {
621:is_ignored_file() {
631:convert2sec() {
647:is_true() {
657:is_false() {
667:apply_sysctl() {
691:__sed_discard_ignored_files='/\(~\|\.bak\|\.old\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
707:strstr "$(cat /proc/cmdline)" "rc.debug" && set -x

 

3、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@srehost ~]#echo /etc/rc.d/init.d/functions |egrep "\<[^/\]*\>$"
/etc/rc.d/init.d/functions

 

4、使用egrep取出上面路径的目录名

[root@srehost ~]#echo /etc/rc.d/init.d/functions |egrep "^/.*/"
/etc/rc.d/init.d/functions


5、统计last命令中以root登录的每个主机IP地址登录次数

[root@srehost jin]#cat last.log | grep -E "^root.*([0-9]{1,3}\.){3}[0-9]{1,3}"| tr -s ' ' | cut -d ' ' -f 1,3| sort |uniq -c | sort -nr
246 root 58.240.68.30
77 root 58.240.68.27
62 root 58.240.68.26
62 root 221.226.39.211
31 root 58.240.68.28
22 root 111.41.18.151
10 root 221.226.39.214
9 root 192.168.1.10
9 root 110.18.63.230
7 root 117.62.172.129
7 root 111.41.18.69
6 root 222.95.248.148
4 root 49.74.148.244
4 root 223.104.116.91
4 root 121.229.178.38
3 root 117.62.175.62
2 root 222.94.198.10
2 root 221.231.222.70
2 root 180.110.31.156
2 root 114.222.244.68
1 root 42.102.145.156
1 root 39.144.248.99
1 root 39.144.248.65
1 root 223.104.146.204
1 root 223.104.113.147
1 root 222.95.250.111
1 root 218.94.159.146
1 root 180.110.31.233
1 root 1.25.91.230
1 root 115.151.190.194
1 root 114.222.244.160
1 root 112.101.195.226


6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[0-9] , [0-9]{2} , 1[0-9]{2}, 2[0-4][0-9], 25[0-5]


7、显示ifconfig命令结果中所有IPv4地址

[root@srehost jin]#ifconfig |egrep -o "([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}"
192.168.81.11
255.255.255.0
192.168.81.255
127.0.0.1
255.0.0.0
[root@srehost jin]#ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.11 netmask 255.255.255.0 broadcast 192.168.81.255
inet6 fe80::20c:29ff:febb:bbc2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:bb:bb:c2 txqueuelen 1000 (Ethernet)
RX packets 29841 bytes 2725303 (2.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20997 bytes 2812617 (2.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 452 bytes 35716 (34.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 452 bytes 35716 (34.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面扩展正则表达式练习