Linux练习(Part4:文本处理工具和正则表达式)
目录
- 练习1
- 练习2
- 正则表达式练习
- 1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
- 2、显示/etc/passwd文件中不以/bin/bash结尾的行
- 3、显示用户rpc默认的shell程序
- 4、找出/etc/passwd中的两位或三位数
- 5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
- 6、找出"netstat -tan"命令结果中以LISTEN后跟任意多个空白字符结尾的行
- 7、显示CentOS7上所有UID小于1000以内的用户名和UID
- 8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin);找出/etc/passwd用户名和shell同名的行
- 9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
- 扩展正则表达式练习
- 1、显示三个用户root、mage、wang的UID和默认shell
- 2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
- 3、使用egrep取出/etc/rc.d/init.d/functions中其基名
- 4、使用egrep取出上面路径的目录名
- 5、统计last命令中以root登录的每个主机IP地址登录次数
- 6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
- 7、显示ifconfig命令结果中所有IPv4地址
- 8、将此字符串: welcome to magedu linux中的每个字符去重并排序,重复次数多的排到前面

练习1
1、在vim中设置tab缩进为4个字符
#显示Tab和换行符^|和$显示
启用:set list
禁用:set nolist
#Tab用指定空格的个数代替(例:4个)
启用:set tabstop=4
简写:set ts=4
2、复制/etc/rc.d/initd/functions文件至/tmp目录,替换/tmp/functions文件中的/etc/sysconfig/init为/var/log
#vi查找并替换
:s(substitute)命令用来查找和替换字符串。语法如下:
:{作用范围}s/{目标}/{替换}/{替换标志}
例如:%s/foo/bar/g会在全局范围(%)查找foo并替换为bar,所有出现都会被替换(g)。
查找替换中的分隔符/可替换为其它字符,如:#,@
%s#/etc/sysconfig/init#/var/log#g
3、删除/tmp/functions文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号
:%s/^# / /
面试题:有两个文件,a.txt与b.txt,合并两个文件,并输出时确保每个数字也唯一
cat a.txt b.txt |sort -n|uniq
练习2
1、找出ifconfig“网卡名”命令结果中本机的IPv4地址
[10:38:15 root@Centos8 /data]#ifconfig ens33|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
100.0.0.120
#-E 使用ERE,相当于egrep
#-o 仅显示匹配到的字符串
或
[12:20:13 root@Centos8 /data]#ifconfig ens33 | tr -s " " |cut -d " " -f 3|head -2|tail -1
100.0.0.120
2、查出分区空间使用率的最大百分比值
[10:43:50 root@Centos8 /data]#df |grep /dev/sd |tr -s " " %|cut -d% -f5|sort -nr|head -1
21
或
[12:18:31 root@Centos8 /data]#df | tr -s " " "%" |cut -d% -f 5|tail -n +3 |sort -nr|head -1
21
3、查出用户UID最大值的用户名、UID及shell类型
[10:57:00 root@Centos8 /data]#cat /etc/passwd |cut -d: -f1,3,7|sort -t: -k2 -nr|head -1
nobody:65534:/sbin/nologin
4、查出/tmp的权限,以数字方式显示
[11:22:48 root@Centos8 /data]#stat /tmp/ |grep Uid|tr -s " " %|cut -d% -f2|grep -oE [0-9]{4}
1777
或
[12:21:39 root@Centos8 /data]#stat /tmp |tr -s " " | cut -d" " -f 2 |head -4 |tail -1 |cut -d "/" -f1 |cut -d"(" -f2
1777
5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
ss -nt |tail -n +2 |tr -s ' ' : |cut -d: -f6 |sort |uniq -c |sort -nr |head -2
正则表达式练习
1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
[13:07:07 root@Centos8 /data]#cat /proc/meminfo | grep "^[S\|s]"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 19064 kB
Slab: 172300 kB
SReclaimable: 63376 kB
SUnreclaim: 108924 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[13:09:53 root@Centos8 /data]#cat /proc/meminfo | grep -i ^s
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 19064 kB
Slab: 172308 kB
SReclaimable: 63376 kB
SUnreclaim: 108932 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
2、显示/etc/passwd文件中不以/bin/bash结尾的行
[13:19:15 root@Centos8 /data]#cat /etc/passwd |grep -v $'/bin/bash'
#-v 显示不被pattern匹配到的行
3、显示用户rpc默认的shell程序
[13:21:37 root@Centos8 /data]#cat /etc/passwd |grep -w rpc|cut -d":" -f7
/sbin/nologin
4、找出/etc/passwd中的两位或三位数
[13:32:36 root@Centos8 /data]#grep -ow '[0-9]\{2,3\}' /etc/passwd
5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
cat /etc/grub2.cfg |grep '^[[:space:]]\+'
6、找出"netstat -tan"命令结果中以LISTEN后跟任意多个空白字符结尾的行
[20:09:53 root@Centos8 /data]#netstat -tan |grep 'LISTEN[[:space:]]\+$'
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 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 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 ::1:6010 :::* LISTEN
7、显示CentOS7上所有UID小于1000以内的用户名和UID
cat /etc/passwd |cut -d: -f1,3|grep '^.*:[0-9]\{1,3\}$'
cut -d: -f1,3 /etc/passwd|grep '^.*\:[0-9]\{1,3\}$'
8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin);找出/etc/passwd用户名和shell同名的行
[20:38:00 root@Centos8 /data]#useradd -s /sbin/nologin bash
[20:39:02 root@Centos8 /data]#getent passwd bash
bash:x:1014:1017::/home/bash:/sbin/nologin
[20:39:27 root@Centos8 /data]#useradd -s /sbin/nologin testbash
[20:39:47 root@Centos8 /data]#getent passwd testbash
testbash:x:1015:1015::/home/testbash:/sbin/nologin
[20:39:53 root@Centos8 /data]#useradd -s /sbin/nologin sh
[20:39:59 root@Centos8 /data]#getent passwd sh
sh:x:1016:1018::/home/sh:/sbin/nologin
[20:40:04 root@Centos8 /data]#useradd -s /sbin/nologin nologin
[20:40:19 root@Centos8 /data]#getent passwd nologin
nologin:x:1017:1019::/home/nologin:/sbin/nologin
[20:40:26 root@Centos8 /data]#grep '\(^.*\):.*\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
nologin:x:1017:1019::/home/nologin:/sbin/nologin
9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
[20:51:13 root@Centos8 /data]#df |grep -o '[0-9]\{1,3\}%'|sort -nr
21%
5%
3%
1%
1%
1%
0%
0%
0%
扩展正则表达式练习
1、显示三个用户root、mage、wang的UID和默认shell
[20:58:28 root@Centos8 /data]#cat /etc/passwd |cut -d: -f1,3,7|grep -e '^root' -e '^mage' -e '^wang'
root:0:/bin/bash
mage:1000:/bin/bash
wang:1013:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
cat /etc/rc.d/init.d/functions |egrep -o "^.*[^[:space:]]\(\)"
cat /etc/rc.d/init.d/functions |egrep "^.*\>\(\)"
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
[22:02:13 root@Centos8 /data]#echo /etc/rc.d/init.d/functions | egrep -o "[^/]+/?$"
functions
4、使用egrep取出上面路径的目录名
echo /etc/rc.d/init.d/functions | egrep -o "^/.*/\b"
5、统计last命令中以root登录的每个主机IP地址登录次数
[22:20:47 root@Centos8 /data]#last |grep ^root |grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'|uniq -c
2 100.0.0.200
47 100.0.0.2
6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
1 \b[0-9]\b 0-9
2 \b[0-9]{2}\b 10-99
3 \b[1][0-9]{2}\b 100-199
4 \b[2][0-4][0-9]\b 200-249
5 \b[2][5][0-5]\b 250-255
7、显示ifconfig命令结果中所有IPv4地址
[22:27:49 root@Centos8 /data]#ifconfig |grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"
100.0.0.120
255.255.255.0
100.0.0.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
8、将此字符串: welcome to magedu linux中的每个字符去重并排序,重复次数多的排到前面
echo "welcome to magedu linux" |grep -o "."|sort |uniq -c|sort -nr
范例:面试题,算出所有人的年龄总和
[22:52:27 root@Centos8 /data]#cat nianling.txt |cut -d= -f2|tr "\n" "+"| grep -Eo ".*[0-9]"|bc
60
[22:55:23 root@Centos8 /data]#grep -Eo "[0-9]+" nianling.txt |tr '\n' +|grep -oE '.*[0-9]'|bc
60