6.文本处理工具和正则表达式练习

VIM

1、在vim中设置tab缩进为4个字符

[root@localhost ~]# cat ~/.vimrc
#!/bin/bash
set et
set ts=4

2、复制/etc/rc.d/init.d/functions文件至/tmp目录,替换/tmp/functions文件中的/etc/sysconfig/init为/var/log

[root@localhost ~]# cp /etc/rc.d/init.d/functions /tmp/
[root@localhost ~]# vim /tmp/functions
:%s@/etc/sysconfig/init@/var/log@g

3、删除/tmp/functions文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号

[root@localhost ~]# vim /tmp/functions
:%s/^# */\1/g

文件处理工具

1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址

[root@localhost ~]# ifconfig ens32 | tail -n+2 | tr -s ' ' | cut -d' ' -f3 | head -1

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

[root@localhost ~]# df | tail -n+2 | tr -s ' '| cut -d' ' -f5 | sort -nr | head -1

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

[root@internet ~]# cat /etc/passwd | cut -d: -f1,3,7|sort -t: -k2 -nr|head -1

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

[root@localhost ~]# stat /tmp/ | tail -n+4 | head -1 | cut -d ' ' -f2 | tr -d "()" | cut -d "/" -f1

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

[root@localhost ~]# netstat -nt | tail -n+3 | tr -s ' ' | cut -d' ' -f5 | cut -d: -f1 | sort -nr| uniq -c

正则表达式、grep

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

[root@localhost ~]# grep -E "^[s|S]" /proc/meminfo
[root@localhost ~]# grep -E "^s|^S" /proc/meminfo

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

[root@localhost ~]# grep -v "/bin/bash$" /etc/passwd

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

[root@localhost ~]# grep "^rpc" /etc/passwd | grep -Eo "/[a-z]*/[a-z]*$"

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

[root@localhost ~]# grep -Eo ":[0-9]{2,3}:" /etc/passwd | tr -d :

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

[root@localhost ~]# grep -E "^ {1,}[^ ]" /etc/grub2.cfg

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

[root@localhost ~]# netstat -tan | grep -E "LISTEN *"

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

[root@localhost ~]# grep -E ":[0-9]{1,3}:" /etc/passwd | cut -d: -f1,3

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

[root@localhost ~]# echo bash testbash basher sh nologin | xargs -n1 useradd
[root@localhost ~]# usermod -s /sbin/nologin nologin
[root@localhost ~]# grep "^sh:" /etc/passwd

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

[root@localhost ~]# df | grep -E "/dev/sd" | grep -Eo "[0-9]+%" | tr -d % | sort -nr

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

[root@localhost ~]# egrep "^(root|mage|wang)" /etc/passwd | cut -d: -f1,3

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

[root@localhost ~]# egrep "^[a-zA-Z_]*\(\)" /etc/rc.d/init.d/functions

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

[root@localhost ~]# echo /etc/rc.d/init.d/functions | egrep -o "[a-z]*$"

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

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

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

[root@localhost ~]# last | egrep "^root" | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | uniq -c

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

[root@localhost ~]# echo 0 |egrep "^[0-9]$"
[root@localhost ~]# echo 99 |egrep "^[1-9][0-9]$"
[root@localhost ~]# echo 199 |egrep "^1[0-9][0-9]$"
[root@localhost ~]# echo 200 |egrep "^2[0-4][0-9]$"
[root@localhost ~]# echo 255 |egrep "^25[0-5]$"

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

[root@localhost ~]# ifconfig | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"

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

[root@localhost ~]# echo welcome to magedu linux | tr -d ' '| grep -o '.' | sort | uniq -c | sort -nr

sed

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

[root@localhost ~]# sed -nE "s/^[[:space:]]+(.*)/\1/p" /etc/grub2.cfg

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

[root@localhost ~]# sed -nE "s/^# +(.*)/\1/p" /etc/fstab

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

[root@localhost ~]# sed -nE "s/(.*)/#\1/p" /root/install.log

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

[root@localhost ~]# sed -nE "s/^[^#](.*)/#\1/p" /etc/fstab

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

[root@localhost ~]# echo /etc/fstab | sed -nE "s/(\/.*\/).*$/\1/p"
[root@localhost ~]# echo /etc/fstab | sed -nE "s/\/.*\/(.*$)/\1/p"

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

[root@localhost ~]# ifconfig ens32 | sed -nE "2s#^[^0-9]+([0-9.]+).*#\1#p"

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

[root@localhost ~]# ls /mnt/Packages/ | sed -nE "s/.*\.(.*)\.rpm/\1/p"|sort|uniq -c

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

[root@localhost ~]# grep -Eo "[a-zA-Z]+" /etc/init.d/functions|sort|uniq -c|sort -n
[root@localhost ~]# sed -E 's/[^[:alpha:]]+/\n/g' /etc/init.d/functions|sort|uniq -c|sort -n

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

[root@localhost ~]# seq 10 |sed "1~2N;s/\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

[root@localhost data]# awk -F'[ .]' '{print $2}' host_list.log

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

[root@localhost data]# awk -F' +' '/^(\/dev|UUID)/{print $3}' /etc/fstab | uniq -c

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

[root@localhost data]# awk '{for(i=1;i<NF;i++){count[$i]++}}END{for(i in count){print i,count[i]}}' /etc/fstab

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

[root@localhost data]# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw" | awk 'gsub(/[^0-9]/,"",$0){print $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

[root@localhost data]# awk -F'\/' '{url[$3]++}END{for(i in url){print url[i],i}}' url.log | sort -nr

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|

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值