Linux 第四次练习(文本处理工具grep,sed.awk)

正则表达式练习

1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
cat /proc/meminfo | grep -i ^s
cat /proc/meminfo | grep -iv ^[^s]

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

grep -v /bin/bash$ /etc/passwd

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

grep ^rpc /etc/passwd | cut -d: -f7

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

grep "\<[0-9]\{2,3\}\>" /etc/passwd

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

grep "^[ ]\+[^ ]" /etc/grub2.cfg

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

netstat -tan | grep "LISTEN[ ]\+"

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

grep "\<[0-9]\{1,3\}\>" /etc/passwd | cut -d: -f1,3

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

for user in bash testbash basher sh nologin;do useradd $user -s /sbin/nologin;done
grep "^\([a-z]*\>\).*\1$" /etc/passwd

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

df | grep ^/dev | tr -s " " % | cut -d"%" -f5 | sort -nr

 

扩展正则表达式练习

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

egrep "^(root|mage|wang)" /etc/passwd | cut -d: -f3,7

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

egrep "^[a-z|_]+\>\(\).*" /etc/rc.d/init.d/functions

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

echo /etc/rc.d/init.d/functions | egrep -o [^/]+$

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

echo /etc/rc.d/init.d/functions | egrep -o "^.*/"

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

last | egrep ^root.*[0-9.]{3}[0-9] | tr -s " " | cut -d" " -f 3 | sort -n | uniq -c

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

for i in {0..255};do echo $i >> 0-255.txt ;done
egrep "\<[0-9]\>" 0-255.txt
egrep "\<[1-9][0-9]\>" 0-255.txt
egrep "\<[1][0-9][0-9]\>" 0-255.txt
egrep "\<[2][0-4][0-9]\>" 0-255.txt
egrep "\<[2][5][0-5]\>" 0-255.txt

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

ifconfig | egrep -o "\<[0-9]+\>.\<[0-9]+\>.\<[0-9]+\>.\<[0-9]+\>"

18、将此字符串:welcometomagedulinux中的每个字符去重并排序,重复次数多的排到前面

echo welcometomagedulinux | egrep -o [a-z] | sort | uniq -c | sort -nr

 

sed练习

19、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符

sed -ri.bak '/^ /s@[ ]+(.*)@\1@' /etc/grub2.cfg

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

sed -ri.bak '/^#[ ]/s@^#[ ](.*)@\1@' /etc/fstab

21、在centos6系统/root/install.log每一行行首增加#号

sed -ri.bak 's@(.*)@#\1@' install.log

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

sed -ri.bak '/^[^#]/s@(.*)@#\1@p' /etc/fstab

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

echo /etc/fstab | sed -rn 's@.*/([a-z]+)$@\1@p'

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

sed -nr '/^ID=/s@.*="?([a-z]+)"?@\1@p' /etc/os-release

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

26、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)

egrep -o "\<[a-z]+\>" /etc/init.d/functions | sort | uniq -c | sort -nr
sed -r 's@[^[:alpha:]]+@\n@g' /etc/init.d/functions | sort | uniq -c | sort -nr

27、将文本文件的n和n+1行合并为一行,n为奇数行

 

 

awk练习

1、文件host_list.log如下格式,请提取”.magedu.com”前面的主机名部分并写入到回到该文件中
1 www.magedu.com
2 blog.magedu.com
3 study.magedu.com
4 linux.magedu.com
5 python.magedu.com
......
999 study.magedu.com
awk -F"[ .]" '{print $2}' host_list.log >> host_list.log 

2、统计文件中每个文件系统类型出现的次数

awk '/^[^#]/{print $3}' /etc/fstab | sort | uniq -c | sort -r

3、统计/etc/fstab文件中每个单词出现的次数

awk -v RS="[/ ()=,.']" '/^[^ ]/{print $0}' /etc/fstab | sort | uniq -c | sort -r

4、提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有数字

echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw" | awk 'gsub(/[^0-9]/,""),$0'

5、有一文件记录了1-100000之间随机的整数共5000个,存储的格式100,50,35,89…请取出其中最大和最小的整数

6、解决Dos攻击生产案例:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔5分钟。防火墙命令为:iptables -A INPUT -s IP -j REJECT

7、将以下文件内容中FQDN取出并根据其进行计数从高到低排序
http://mail.magedu.com/index.html
http://www.magedu.com/test.html
http://study.magedu.com/index.html
http://blog.magedu.com/index.html
http://www.magedu.com/images/logo.jpg
http://blog.magedu.com/20080102.html
http://www.magedu.com/images/magedu.jpg
awk -F"/" '{print $3}' web.txt | sort | uniq -c | sort -rn
参考答案:
[root@centos8~]#awk -F"/" '{url[$3]++}END{for(i in url){print url[i],i}}'url.log | sort-nr
3www.magedu.com
2blog.magedu.com
1study.magedu.com
1mail.magedu.com

8、将以下文本以inode为标记,对inode相同的counts进行累加,并且统计出同一inode中,beginnumber的最小值和endnumber的最大值

inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|
输出的结果格式为
310|3337000000|3362120961|10103|
311|3313460102|3362120963|39900|
106|3363120000|3368579999|30000|
参考答案:
[root@centos8~]#cat awktest.txt
inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|
 [root@centos8~]#awk -F '|' '!/^inode/{sum[$1]+=$4;
if(!begin[$1])begin[$1]=$2; else if(begin[$1]>$2)begin[$1]=$2;
if(!end[$1])end[$1]=$3; else if(end[$1]<$3)end[$1]=$3}
END{for(i in sum)print i "|"begin[i]"|"end[i]"|"sum[i]}' awktest.txt
 [root@centos8~]#awk -F'|' -v OFS='|' '/^[0-9]/{inode[$1]++; if(!bn[$1])
{bn[$1]=$2}else if(bn[$1]>$2){bn[$1]=$2}; if(en[$1]<$3)en[$1]=$3; cnt[$1]+=$(NF-1)}
END{for(i in inode)print i,bn[i],en[i],cnt[i]}' awktest.txt=y
posted @ 2021-09-20 19:18  陈强。  阅读(687)  评论(0)    收藏  举报