12.27 -第三关练习题之正则练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.27 -第三关练习题之正则练习相关的知识,希望对你有一定的参考价值。
12.27第三关练习题之正则练习
第1章 取出网卡的ip地址(ifconfig)
1.1 定位
[[email protected] oldboy]# ifconfig eth0|sed -n '2p' 方法一:sed
inet addr:10.0.0.201 Bcast:10.0.0.255 Mask:255.255.255.0
[[email protected] oldboy]# ifconfig|awk 'NR==2' 方法二:awk
inet addr:10.0.0.201 Bcast:10.0.0.255 Mask:255.255.255.0
[[email protected] oldboy]# ifconfig eth0|grep 'cast' 方法三:grep
inet addr:10.0.0.201 Bcast:10.0.0.255 Mask:255.255.255.0 找唯一
1.2 解决:
1.2.1 方法一:sed |(管道) 把目标前后替换为空(相当于删除)
[[email protected] oldboy]# ifconfig eth0|sed -n '2p'|sed 's#.*r:##g'|sed 's# B.*##g'
10.0.0.201
1.2.2 方法二:sed ‘|’(或者)
[[email protected] oldboy]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*r:| B.*$##g'
10.0.0.201 注意把空格也去掉
1.2.3 方法三:sed(后向引用)
[[email protected] oldboy]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*r:(.*)B.*$#\1#g'
10.0.0.201
终结命令
[[email protected] oldboy]# ifconfig eth0 |sed -rn '2s#^.*r:(.*) Bca.*$#\1#gp'
10.0.0.201
1.2.4 方法四:awk -F
[[email protected] oldboy]# ifconfig eth0 |awk 'NR==2'|awk -F "[ :]+" '{print $4}'
10.0.0.201
终结命令
'NR==2{print $4}'
‘条件 {命令}’
[[email protected] oldboy]# ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}'
10.0.0.201
1.2.5 方法五grep/egrep
[[email protected] oldboy]# ifconfig eth0|awk 'NR==2'|egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' -o
10.0.0.200
10.0.0.255
255.255.255.0
[[email protected] oldboy]# ifconfig eth0|awk 'NR==2'|egrep '([0-9]+\.){3}[0-9]+' -o
10.0.0.200
10.0.0.255
255.255.255.0
[[email protected] oldboy]# ifconfig eth0|awk 'NR==2'|egrep '([0-9]+\.?){4}' -o
10.0.0.200
10.0.0.255
255.255.255.0
方法六:cut 1. cut -d指定分隔符 -f 指定某一列
[[email protected] oldboy]# ifconfig eth0|awk 'NR==2'|cut -d ":" -f2
10.0.0.201 Bcast
[[email protected] oldboy]# ifconfig eth0|awk 'NR==2'|cut -d ":" -f2|cut -d " " -f1
10.0.0.201
2. cut sed命令把:替换为空格, cut指定空格为分隔符
[[email protected] oldboy]# ifconfig eth0|awk 'NR==2'|sed 's#:# #g'|cut -d " " -f13
10.0.0.201
1.3 练习:执行ip a s eth0命令取出ip
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9c:08:2c brd ff:ff:ff:ff:ff:ff
inet 10.0.0.201/24 brd 10.0.0.255 scope global eth0
inet6 fe80::20c:29ff:fe9c:82c/64 scope link
valid_lft forever preferred_lft forever
方法一: awk终极版
[[email protected] ~]# ip a s eth0 |awk '/eth0$/'
inet 10.0.0.201/24 brd 10.0.0.255 scope global eth0
[[email protected] ~]# ip a s eth0 |awk -F "[ /]+" '/eth0$/{print $3}'
10.0.0.201
方法二: awk
[[email protected] oldboy]# ip a s eth0 |awk 'NR==3'|awk -F “[ /]+” '{print $3}'
10.0.0.200
方法三:sed
[[email protected] oldboy]# ip a s eth0 |awk 'NR==3'|sed -r 's#^.*t (.*)/.*$#\1#g'
10.0.0.200
1.4 总结:
1.4.1 理解-F指定分隔符
—F “[ :]+”
先找出[ :]+ 正则匹配到什么内容
egrep ‘[ :]+’
把找出来的内容作为分隔符
1.4.2 理解--连续出现(+)
[[email protected] oldboy]# echo '#####[email protected]@@@@2'
#####[email protected]@@@@2
[[email protected] oldboy]# echo '#####[email protected]@@@@2'|egrep '[#@]'
#####[email protected]@@@@2
[[email protected] oldboy]# echo '#####[email protected]@@@@2'|egrep '[#@]' -o
#
#
#
@
@
@
[[email protected] oldboy]#
[[email protected] oldboy]# echo '#####[email protected]@@@@2'|egrep '[#@]+'
#####[email protected]@@@@2
[[email protected] oldboy]# echo '#####[email protected]@@@@2'|egrep '[#@]+' -o
#####
@@@@@
[[email protected] oldboy]# echo '#####[email protected]@@@@2'|awk -F "[#@]+" '{print $2}'
1
第2章 过滤出oldboy 和31333741
方法一:
[[email protected] ~]# echo "I am oldboy,myqq is 31333741"|awk -F "[ ,]" '{print $3,$6}'
oldboy 31333741
方法二:
[[email protected] ~]# echo "I am oldboy,myqq is 31333741"|sed -r 's#.*m (.*),.*s (.*)#\1 \2#g'
oldboy 31333741
第3章 取出/etc/hosts 文件权限
[[email protected] oldboy]# stat /etc/hosts
File: `/etc/hosts'
Size: 177 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 260127 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-12-21 23:29:37.849768706 +0800
Modify: 2017-05-20 18:18:04.258143406 +0800
方法一:sed
[[email protected] oldboy]# stat /etc/hosts|sed -n '4p'|sed -r 's#^.*\((.*)/-.*$#\1#g'
0644
方法二:sed终极版
[[email protected] oldboy]# stat /etc/hosts|sed -rn '4s#^.*\((.*)/-.*$#\1#gp'
0644
方法三:awk
[[email protected] oldboy]# stat /etc/hosts|awk -F "[(/]" 'NR==4{print $2}'
0644
方法四:命令本身选项
[[email protected] oldboy]# stat -c%a /etc/hosts
644
第4章 已知/oldboy/test.txt 文件内容为:
oldboy
xizi
xiaochao
请问如何把文件中的空格过滤掉(要求命令行实现) ^$ . [a-z]
方法一:
[[email protected]]# grep -v '^$' test.txt
oldboy
xizi
xiaochao
方法二:
grep ‘.’test.txt
方法三:
[[email protected] oldboy]# grep '^[a-zA-Z]' test.txt
oldboy
xizi
xiaochao
方法四: awk ! 取反
awk '!/^$/' test.txt
方法五: sed d 删除
sed '/^$/d' test.txt
方法六:
[[email protected] oldboy]# sed -rn '/[a-z]/p' test.txt
ldboy
xizi
xiaochao
方法七:
[[email protected] oldboy]# awk '/[a-z]/' test.txt
ldboy
xizi
xiaochao
方法八:
[[email protected] oldboy]# sed -n '/./p' test.txt
ldboy
xizi
xiaochao
方法九:
[[email protected] oldboy]# sed -n '/./p' test.txt
ldboy
xizi
xiaochao
4.1 总结:
grep 过滤 找东西
sed 过滤 取行 替换 修改文件内容
awk 过滤 取列 计算统计
第5章 已知/oldboy/ett.txt 文件内容为:
oldboy
olldboooy
test
请使用 grep 或 egrep 正则匹配的方式过滤出前两行内容
方法一:
[[email protected] oldboy]# grep '^o' ett.txt
oldboy
olldboooy
方法二:
[[email protected] oldboy]# awk '/^o/' ett.txt
oldboy
olldboooy
方法三:
[[email protected] oldboy]# sed -n '/^o/p' ett.txt
oldboy
olldboooy
方法四:
[[email protected] oldboy]# egrep 'ol+dbo+y' ett.txt
oldboy
olldboooy
方法五:
[[email protected] oldboy]# awk '/ol+dbo+y/' ett.txt
oldboy
olldboooy
方法六:
[[email protected] oldboy]# sed -nr '/ol+dbo+y/p' ett.txt
oldboy
olldboooy
方法七:
[[email protected] oldboy]# awk '/^[a-o]/' ett.txt
oldboy
olldboooy
方法八:sed显示某一行用法
[[email protected] oldboy]# sed -n '1,2p' ett.txt
oldboy
olldboooy
方法九:awk显示某一行用法
[[email protected] oldboy]# awk 'NR= =1,NR= =2' ett.txt
oldboy
olldboooy
方法十:sed--排除t开头的行
[[email protected] oldboy]# sed -n '/^[^t]/p' ett.txt
oldboy
olldboooy
方法十一:find --排除t开头的行
1.[[email protected] oldboy]# grep '^[^t]' ett.txt
oldboy
olldboooy
2.[[email protected] oldboy]# grep -v '^[t]' ett.txt
oldboy
olldboooy
第6章 扩展:
6.0.1 系统中默认有centos7没有ifconfig命令,需要yum安装
ip a s eth0=====ip address show eth0 查看ip地址
6.0.2 单引号、双引号及不加引号的简单区别
1.单引号 所见即所得
[[email protected] oldboy]# echo '$LANG $(hostname) `pwd` {1..5}'
$LANG $(hostname) `pwd` {1..5}
2.双引号 与单引号类似 特殊符号会被解析 ! $() $ ``
[[email protected] oldboy]# echo "$LANG $(hostname) `pwd` {1..5}"
en_US.UTF-8 oldboyedu43-lnb /oldboy {1..5}
3.不加引号 和双引号类似 支持通配符
[[email protected] oldboy]# echo $LANG $(hostname) `pwd` {1..5}
en_US.UTF-8 oldboyedu43-lnb /oldboy 1 2 3 4 5
以上是关于12.27 -第三关练习题之正则练习的主要内容,如果未能解决你的问题,请参考以下文章