[[email protected] exam]# echo ‘<123456>‘ | sed ‘s#123456#123456#g‘
<123456>
[[email protected] exam]# echo ‘<123456>‘ | sed ‘s#123456#<123456>#g‘
<<123456>>
[[email protected] exam]#
echo 123456
想要的结果:<123456>
[[email protected] exam]# echo ‘123456‘ | sed -r ‘s#(.*)#<\1>#g‘
<123456>
[[email protected] exam]# echo 123456 | sed -r ‘s#(.*)#<\1>#g‘
<123456>
想要的结果:<1><2><3><4><5><6>
[[email protected] exam]# echo 123456 | sed -r ‘s#(.)#<\1>#g‘
<1><2><3><4><5><6>
echo ‘11222233333344442222266668888990000999‘
想要结果
11
2222
333333
4444
22222
6666
8888
99
0000
999
提取IP地址
[[email protected] exam]# ip a s em1 | sed -n 3p |sed -r ‘s#^.*et (.*)/.*$#\1#g‘
172.16.10.27
[[email protected] exam]# ip a s em1 | sed -rn ‘3s#^.*et (.*)/.*$#\1#gp‘
172.16.10.27
提取ifconfig em1 中ip地址和掩码:
[[email protected] exam]# ifconfig em1 |sed -rn 2p
inet 172.16.10.27 netmask 255.255.255.0 broadcast 172.16.10.255
[[email protected] exam]# ifconfig em1 |sed -rn ‘2s#^.*et (.*) .*k (.*) br.*$#\1 \2#gp‘
172.16.10.27 255.255.255.0
awk命令格式
参数
-F
-v
修改或创建awk变量
-F= -vFS=
awk ‘找谁{干啥}‘
找谁
模式
pattern
条件
干啥
动作
action
命令
显示第1行的第1列
[[email protected] exam]# ip a s em1
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 18:66:da:eb:0c:14 brd ff:ff:ff:ff:ff:ff
inet 172.16.10.27/24 brd 172.16.10.255 scope global em1
valid_lft forever preferred_lft forever
inet6 fe80::1a66:daff:feeb:c14/64 scope link
valid_lft forever preferred_lft forever
[[email protected] exam]# ip a s em1 |awk ‘NR==1{print $1}‘
2:
awk执行过程
‘NR==1{print $1}
条件: NR==1 找出第1行
动作: {print $1} 显示第1列
awk 行 与 列
行 record 记录
列 field 区域 字段
NR Number of Record 记录号 行号
[[email protected] exam]# ip a s em1
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 18:66:da:eb:0c:14 brd ff:ff:ff:ff:ff:ff
inet 172.16.10.27/24 brd 172.16.10.255 scope global em1
valid_lft forever preferred_lft forever
inet6 fe80::1a66:daff:feeb:c14/64 scope link
valid_lft forever preferred_lft forever
[[email protected] exam]# ip a s em1 |awk ‘NR==1{print $1}‘
2:
[[email protected] exam]# cat sed.txt
101.oldboy,CEO
102,zhangyao.CTO
103,Alex.COO
104,yy,CFO
105,feixue,CIO
110,lidao,COCO
[[email protected] exam]# awk ‘NR==3‘ /exam/sed.txt
103,Alex.COO
[[email protected] exam]# awk -F ‘,‘ ‘{print $2}‘ /exam/sed.txt
CEO
zhangyao.CTO
Alex.COO
yy
feixue
lidao
-F 指定分隔符 指定新的菜刀
提取 ip a s em1 命令中的IP地址
[[email protected] exam]# ifconfig em1 |awk ‘NR==2{print $2}‘
172.16.10.27
[[email protected] exam]# ip a s em1 |awk ‘NR==3{print $2}‘
172.16.10.27/24
[[email protected] exam]# ip a s em1 |awk -F ‘[ /]+‘ ‘NR==3{print $3}‘
172.16.10.27
取行 NR==1
取列 $数字
NF Number of Field 每行有多少列
$NF 最后一列
$(NF-1) 倒数第2列
{print $1,$2,$3}
{print $NF}
显示 /etc/passwd 第1列,第3列和最后一列
找谁 条件?
原文地址:https://blog.51cto.com/1674389/2443403