文本处理练习

一.文本处理练习正则表达式

一、找出ifconfig命令结果中本机的全部IPv4地址shell

[root@centos7 zhang]# ifconfig |tr -s " " |head -2 |tail -1| cut -d " " -f3

二、查出分区空间使用率的最大百分比值centos

[root@centos7 zhang]# df |cut -c46-48 |sort -nr |head -1

三、查出用户UID最大值的用户名、UID及shell类型bash

[root@centos7 zhang]# cat /etc/passwd |sort -t: -k3 -n |tail -1 |cut -d: -f1,3,7

四、查出/tmp的权限,以数字方式显示ide

[root@centos7 zhang]# stat /tmp |head -4|tail -1 |cut -d "/" -f1 |cut -d "1" -f2

五、统计当前链接本机的每一个远程主机IP的链接数,并按从大到小排序centos7

[root@centos7 zhang]# netstat -nt |cut -d : -f2 |tr -s " " ":" |cut -d: -f2 |sort -r |uniq -c


二.grep练习spa

一、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)排序

[root@centos7 zhang]# grep -i "^s" /proc/meminfo 
或[root@centos7 zhang]# grep "^[Ss]" /proc/meminfo
或[root@centos7 zhang]# egrep "^(S|s)" /proc/meminfo

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

[root@centos7 zhang]# grep  "/bin/bash$" /etc/passwd

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

[root@centos7 zhang]# grep "^rpc\>" /etc/passwd |cut -d: -f7

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

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

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

[root@centos7 zhang]# grep -n "^[[:space:]]\+[^[:space:]]*" /etc/grub2.cfg

六、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

[root@centos7 zhang]# netstat -tan |grep -n "\<LISTEN[[:space:]]*$"

七、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),然后找出/etc/passwd文件中用户名和shell相同的行

[root@localhost testdir]# grep "^\([[:alnum:]]\+\):.*\1$" /etc/passwd
或[root@localhost testdir]# grep "^\(\<.*\>\).*\1$" /etc/passwd



三.egrep练习


一、显示当前系统root、mage或wang用户的UID和默认shell

[root@centos7 zhang]# egrep "(root|zhang)" /etc/passwd |cut -d: -f3,7

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

[root@centos7 zhang]# egrep "^\<([[:alpha:]]|_)+\>\(\).*" /etc/rc.d/init.d/functions

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

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

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

[root@centos7 zhang]# echo  "/etc/rc.d/init.d/fun.ctions" |egrep -o "^.*/"

五、统计以zhang身份登陆的每一个远程主机IP地址的登陆次数

[root@centos7 zhang]# w|egrep "^zhang" |sort -nr |tr -s " " ":"|cut -d: -f3 |uniq -c

六、利用扩展正则表达式分别表示0-九、10-9九、100-19九、200-24九、250-255

"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]"

七、显示ifconfig命令结果中全部IPv4地址

[root@centos7 Packages]# ifconfig |egrep "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"



三.做业

一、用正则表达式表示IP地址

[root@centos7 Packages]# ifconfig |egrep "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"

二、用正则表达式表示手机号11 13 17 15  18 

grep "\<1[13578][0-9]{9}\>"

 

三、用正则表达式表示×××号18

 

egrep "\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>" filename

四、用正则表达式表示邮箱

[root@centos7 testdir]# egrep "\b[[:alnum:]]+(-|_)*[[:alnum:]]\b@([[:alnum:]]+\.)+[[:alnum:]]+" mail


四.sed练习

一、删除/etc/grub2.conf文件中全部以空白开头的行行首的空白字符

[root@centos7 ~]# sed -r 's@^[[:space:]]+@@' /etc/grub2.cfg

二、删除/etc/fstab文件中全部以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

[root@centos7 ~]# sed 's/^#[[:space:]]\+//' /etc/fstab

三、在/etc/passwd每一行行首增长#号

[root@centos7 ~]# sed 's/^/#/' /etc/passwd

四、在/etc/fstab文件中不以#开头的行的行首增长#号

[root@centos7 ~]# sed 's/^[^#]/#/' /etc/fstab
或[root@centos7 ~]# sed -r 's/^([^#].*)/#\1/' /etc/fstab

五、处理/etc/fstab路径,使用sed命令取出其目录名和基名

echo "/etc/fst/sd" | sed -r  's@(.*)/([^/]+/?)@\2@' 取基名
echo "/etc/fst/sd" | sed   -r 's@(.*/)([^/]+/?)$@\1@'取目录名
或[root@centos7 ~]# echo "/etc/fstab/asdd" |sed -r 's@[^/]+\/?$@@' 取目录名
 [root@centos7 ~]# echo "/etc/fstab/asdd" |sed -r 's@^.*/@@'     取基名

六、利用sed 取出ifconfig命令中本机的IPv4地址

[root@centos7 ~]# ifconfig |sed -n '2p' |sed -r 's@^[[:space:]]+inet@@' |sed -r 's@net.*$@@'

七、统计centos安装光盘中Package目录下的全部rpm文件的以.分隔倒数第二个字段的重复次数

[root@centos7 Packages]# ls *.rpm |sed -r 's@.*\.(.*)\.rpm@\1@' |sort |uniq -c
相关文章
相关标签/搜索