为了将RouterOS的dhcp lease纪录整合成一行想要写个script去处理结果碰到问题
纪录档如下
3 D address=10.1.1.1 mac-address=FF:FF:FF:FF:FF:FF
client-id="1:ff:ff:ff:ff:ff:ff" address-lists="" server=dhcp-local
dhcp-option="" status=bound expires-after=7h36m38s last-seen=23m22s
active-address=10.1.1.1 active-mac-address=FF:FF:FF:FF:FF:FF
active-client-id="1:ff:ff:ff:ff:ff:ff" active-server=dhcp-local
host-name="VOIP_PHONE"
*纪录中每一行最后有一个空格,换行后最前面有五个空格
两笔纪录中间有一个空行,这个空行没有空格
简单的说就是要把 20 0A 20 20 20 20 20 变成一个 20,于是我用下面两种方法
sed -i 's/\x0a\x20\x20\x20\x20\x20//g' dhcp-lease.txt
sed -i 's/\x20\x0A//g' dhcp-lease.txt
但结果是只有空格会被取代,换行符号不会被处理( \x0a 改成 \n 也一样)
试了半天只有 tr 可以处理掉换行( \012 ),后来是分成三步骤取代才成功
tr -s "\012" "\011" < dhcp-lease.txt >temp2.txt
sed 's/ \x09 //g' temp2.txt >temp3.txt
sed 's/\x09/\n/g' temp3.txt >temp4.txt
但总觉得这样子不漂亮,是否有更好的解法可以参考?