正则表达式作业练习

正则表达式练习

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

[root@centos7 data]# cat /proc/meminfo | grep  -i "^s"
SwapCached:            0 kB
SwapTotal:       4194300 kB
SwapFree:        4194300 kB
Shmem:              7840 kB
Slab:              81436 kB
SReclaimable:      37284 kB
SUnreclaim:        44152 kB

[root@centos7 data]# cat /proc/meminfo | grep   "^[sS]"
SwapCached:            0 kB
SwapTotal:       4194300 kB
SwapFree:        4194300 kB
Shmem:              7840 kB
Slab:              81436 kB
SReclaimable:      37284 kB
SUnreclaim:        44152 kB

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

[root@centos7 data]# grep -v "/bin/bash$" /etc/passwd

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

[root@centos7 data]# grep "rpc" /etc/passwd
rpc:x:1009:1012::/home/rpc:/bin/bash
[root@centos7 data]# grep "rpc" /etc/passwd | cut -d: -f7
/bin/bash

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

感谢热心同学提供帮助帮忙指正包含问题,指点了词首词尾锚定

[root@centos7 data]# grep -Eo "<[0-9]{2,3}>" /etc/passwd

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

[root@centos7 data]# grep -E "^ " /etc/grub2.cfg 
[root@centos7 data]# grep -E "^[[:space:]]+" /etc/grub2.cfg

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

[root@centos7 data]# netstat -tan | grep "LISTEN" | cat -A
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     $
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     $
tcp6       0      0 :::22                   :::*                    LISTEN     $

更严谨写法

[root@centos7 data]# netstat -tan | grep -E "LISTEN[[:space:]]+" | cat -A
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     $
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     $
tcp6       0      0 :::22                   :::*                    LISTEN     $

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

[root@centos7 data]# grep -E ":[0-9]{1,3}:[0-9]{1,}" /etc/passwd | cut -d: -f1,3
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
saslauth:998
mailnull:47
smmsp:51

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

[root@centos7 data]# grep -E "^([[:alnum:]]+).*1$" /etc/passwd 
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:1010:1013::/home/bash:/bin/bash
sh:x:1013:1016::/home/sh:/bin/sh
nologin:x:1014:1017::/home/nologin:/sbin/nologin

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

[root@centos7 data]# df | grep "^/dev/sd*" | tr -s " " | cut -d" " -f5 | sort -nr
13%
2%
1%

扩展正则表达式练习

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

[root@centos7 data]# grep -E  "^root:|wang:|mage:" /etc/passwd | cut -d: -f3,7
0:/bin/bash
1000:/bin/bash
1016:/bin/bash

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

[root@centos7 data]# grep -E "^[[:alpha:]]+\_?[[:alpha:]]?*(" /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_true() {
is_false() {
apply_sysctl() {

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

[root@centos7 data]# echo "/etc/rc.d/init.d/functions" | egrep -o "[[:alpha:]]+$"
functions

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

[root@centos7 data]# echo "/etc/rc.d/init.d/functions" | egrep  -io "/([[:alpha:]]+.?[[:alpha:]]?/){3}"
/etc/rc.d/init.d/

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

[root@centos7 data]# last | grep "^root" | grep -Eo "([0-9]{1,3}.){1,3}[0-9]" | uniq -c | sort -nr
     16 10.0.0.1

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

[root@centos7 data]# 0-9    "[0-9]{1}"
[root@centos7 data]# 10-99  "[0-9]{2}"
[root@centos7 data]# 100-199  "1[0-9]{2}"
[root@centos7 data]# 200-249  "2[0-4][0-9]"
[root@centos7 data]# 250-255  "25[0-5]"

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

[root@centos7 data]# ifconfig | grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}"
10.0.0.150
255.255.255.0
10.0.0.255
127.0.0.1
255.0.0.0

#排除255
[root@centos7 data]# ifconfig | grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}" | grep -v "^255"
10.0.0.150
10.0.0.255
127.0.0.1

8、将此字符串: welcome to magedu linux中的每个字符去重并排序,重复次数多的排到前面

[root@centos7 data]# echo "welcome to magedu linux"  | grep -o [a-z] | sort | uniq -c | sort -nr
      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

                                                                  

原文地址:https://www.cnblogs.com/tianakong/p/14595745.html