如何使用sed提取包含MAC地址的每个IP address
部分的MAC address
和lease {...}
?
示例输入:
lease 192.168.0.188 {
starts 0 2015/10/18 10:02:20;
ends 0 2015/10/18 10:32:20;
cltt 0 2015/10/18 10:02:20;
binding state active;
next binding state free;
hardware ethernet 2c:44:fd:25:f7:fc;
uid "\001,D\375%\367\374";
client-hostname "708-PC";
}
lease 192.168.0.71 {
starts 0 2015/10/18 10:02:16;
ends 0 2015/10/18 10:02:16;
tstp 0 2015/10/18 10:02:16;
cltt 0 2015/10/18 10:02:16;
binding state abandoned;
next binding state free;
}
示例输出:
192.168.0.188
2c:44:fd:25:f7:fc
我尝试使用:
s/lease // ;s/hardware ethernet // ;s/^ // ;/^[^0-9]/d ;s/[^0-9a-z\:\.][\{]// ;s/\;// ;/^$/d
然而,这并没有正常工作。
答案 0 :(得分:0)
如果您的awk
支持gensub()
,
$ awk -vRS='}' '/lease/ && /hardware ethernet/{r=gensub(/.*lease ([^ ]*).*hardware ethernet ([^;]*).*/,"\\1\n\\2", "g");print r}' file
158.108.30.188
2c:44:fd:25:f7:fc
答案 1 :(得分:0)
使用awk:
awk 'BEGIN {RS="lease "} /hardware ethernet / { match($0,/[[:xdigit:]:]{17}/); print $1; print substr($0, RSTART, RLENGTH) }' your_file
158.108.30.188
2c:44:fd:25:f7:fc
使用sed:
sed -r -n '/lease/{h};/hardware/{H;g;s/lease (.+) \{.*ethernet (.+);/\1\n\2/;p}' your_file
158.108.30.188
2c:44:fd:25:f7:fc