第三关练习题之正则练习
[root@oldboy oldboy]# ifconfig eth0|sed -n '2p' 方法一:sed
inet addr:10.0.0.201 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboy oldboy]# ifconfig|awk 'NR==2' 方法二:awk
inet addr:10.0.0.201 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboy oldboy]# ifconfig eth0|grep 'cast' 方法三:grep
inet addr:10.0.0.201 Bcast:10.0.0.255 Mask:255.255.255.0 找唯一
[root@oldboy oldboy]# ifconfig eth0|sed -n '2p'|sed 's#.*r:##g'|sed 's# B.*##g'
10.0.0.201
[root@oldboy oldboy]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*r:| B.*$##g'
10.0.0.201 注意把空格也去掉
[root@oldboy oldboy]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*r:(.*)B.*$#\1#g'
10.0.0.201
终结命令
[root@oldboy oldboy]# ifconfig eth0 |sed -rn '2s#^.*r:(.*) Bca.*$#\1#gp'
10.0.0.201
[root@oldboy oldboy]# ifconfig eth0 |awk 'NR==2'|awk -F "[ :]+" '{print $4}'
10.0.0.201
终结命令
'NR==2{print $4}'
‘条件 {命令}’
[root@oldboy oldboy]# ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}'
10.0.0.201
[root@oldboyedu43-lnb 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
[root@oldboyedu43-lnb 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
[root@oldboyedu43-lnb 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 指定某一列
[root@oldboy oldboy]# ifconfig eth0|awk 'NR==2'|cut -d ":" -f2
10.0.0.201 Bcast
[root@oldboy oldboy]# ifconfig eth0|awk 'NR==2'|cut -d ":" -f2|cut -d " " -f1
10.0.0.201
2. cut sed命令把:替换为空格, cut指定空格为分隔符
[root@oldboy oldboy]# ifconfig eth0|awk 'NR==2'|sed 's#:# #g'|cut -d " " -f13
10.0.0.201
[root@oldboy ~]# 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终极版
[root@oldboy ~]# ip a s eth0 |awk '/eth0$/'
inet 10.0.0.201/24 brd 10.0.0.255 scope global eth0
[root@oldboy ~]# ip a s eth0 |awk -F "[ /]+" '/eth0$/{print $3}'
10.0.0.201
方法二: awk
[root@oldboyedu43-lnb oldboy]# ip a s eth0 |awk 'NR==3'|awk -F “[ /]+” '{print $3}'
10.0.0.200
方法三:sed
[root@oldboyedu43-lnb oldboy]# ip a s eth0 |awk 'NR==3'|sed -r 's#^.*t (.*)/.*$#\1#g'
10.0.0.200
—F “[ :]+”
先找出[ :]+ 正则匹配到什么内容
egrep ‘[ :]+’
把找出来的内容作为分隔符
[root@oldboyedu43-lnb oldboy]# echo '#####1@@@@@2'
#####1@@@@@2
[root@oldboyedu43-lnb oldboy]# echo '#####1@@@@@2'|egrep '[#@]'
#####1@@@@@2
[root@oldboyedu43-lnb oldboy]# echo '#####1@@@@@2'|egrep '[#@]' -o
#
#
#
@
@
@
[root@oldboyedu43-lnb oldboy]#
[root@oldboyedu43-lnb oldboy]# echo '#####1@@@@@2'|egrep '[#@]+'
#####1@@@@@2
[root@oldboyedu43-lnb oldboy]# echo '#####1@@@@@2'|egrep '[#@]+' -o
#####
@@@@@
[root@oldboyedu43-lnb oldboy]# echo '#####1@@@@@2'|awk -F "[#@]+" '{print $2}'
1
方法一:
[root@oldboy ~]# echo "I am oldboy,myqq is 31333741"|awk -F "[ ,]" '{print $3,$6}'
oldboy 31333741
方法二:
[root@oldboy ~]# echo "I am oldboy,myqq is 31333741"|sed -r 's#.*m (.*),.*s (.*)#\1 \2#g'
oldboy 31333741
[root@oldboy 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
[root@oldboy oldboy]# stat /etc/hosts|sed -n '4p'|sed -r 's#^.*\((.*)/-.*$#\1#g'
0644
方法二:sed终极版
[root@oldboy oldboy]# stat /etc/hosts|sed -rn '4s#^.*\((.*)/-.*$#\1#gp'
0644
方法三:awk
[root@oldboy oldboy]# stat /etc/hosts|awk -F "[(/]" 'NR==4{print $2}'
0644
方法四:命令本身选项
[root@oldboy oldboy]# stat -c%a /etc/hosts
644
oldboy
xizi
xiaochao
请问如何把文件中的空格过滤掉(要求命令行实现) ^$ . [a-z]
方法一:
[root@oldboy]# grep -v '^$' test.txt
oldboy
xizi
xiaochao
方法二:
grep ‘.’test.txt
方法三:
[root@oldboyedu43-lnb oldboy]# grep '^[a-zA-Z]' test.txt
oldboy
xizi
xiaochao
方法四: awk ! 取反
awk '!/^$/' test.txt
方法五: sed d 删除
sed '/^$/d' test.txt
方法六:
[root@oldboy oldboy]# sed -rn '/[a-z]/p' test.txt
ldboy
xizi
xiaochao
方法七:
[root@oldboy oldboy]# awk '/[a-z]/' test.txt
ldboy
xizi
xiaochao
方法八:
[root@oldboy oldboy]# sed -n '/./p' test.txt
ldboy
xizi
xiaochao
方法九:
[root@oldboy oldboy]# sed -n '/./p' test.txt
ldboy
xizi
xiaochao
grep 过滤 找东西
sed 过滤 取行 替换 修改文件内容
awk 过滤 取列 计算统计
oldboy
olldboooy
test
请使用 grep 或 egrep 正则匹配的方式过滤出前两行内容
方法一:
[root@oldboyedu43-lnb oldboy]# grep '^o' ett.txt
oldboy
olldboooy
方法二:
[root@oldboyedu43-lnb oldboy]# awk '/^o/' ett.txt
oldboy
olldboooy
方法三:
[root@oldboyedu43-lnb oldboy]# sed -n '/^o/p' ett.txt
oldboy
olldboooy
方法四:
[root@oldboyedu43-lnb oldboy]# egrep 'ol+dbo+y' ett.txt
oldboy
olldboooy
方法五:
[root@oldboyedu43-lnb oldboy]# awk '/ol+dbo+y/' ett.txt
oldboy
olldboooy
方法六:
[root@oldboyedu43-lnb oldboy]# sed -nr '/ol+dbo+y/p' ett.txt
oldboy
olldboooy
方法七:
[root@oldboy oldboy]# awk '/^[a-o]/' ett.txt
oldboy
olldboooy
[root@oldboy oldboy]# sed -n '1,2p' ett.txt
oldboy
olldboooy
方法九:awk显示某一行用法
[root@oldboy oldboy]# awk 'NR= =1,NR= =2' ett.txt
oldboy
olldboooy
方法十:sed--排除t开头的行
[root@oldboy oldboy]# sed -n '/^[^t]/p' ett.txt
oldboy
olldboooy
方法十一:find --排除t开头的行
1.[root@oldboy oldboy]# grep '^[^t]' ett.txt
oldboy
olldboooy
2.[root@oldboy oldboy]# grep -v '^[t]' ett.txt
oldboy
olldboooy
ip a s eth0=====ip address show eth0 查看ip地址
1.单引号 所见即所得
[root@oldboyedu43-lnb oldboy]# echo '$LANG $(hostname) `pwd` {1..5}'
$LANG $(hostname) `pwd` {1..5}
2.双引号 与单引号类似 特殊符号会被解析 ! $() $ ``
[root@oldboyedu43-lnb oldboy]# echo "$LANG $(hostname) `pwd` {1..5}"
en_US.UTF-8 oldboyedu43-lnb /oldboy {1..5}
3.不加引号 和双引号类似 支持通配符
[root@oldboyedu43-lnb oldboy]# echo $LANG $(hostname) `pwd` {1..5}
en_US.UTF-8 oldboyedu43-lnb /oldboy 1 2 3 4 5
原文:http://blog.51cto.com/13517084/2056428