使用 awk 将表单文本块写入表(openwrt 文件--> # cat /etc/config/dhcp)

使用 awk 将表单文本块写入表(openwrt 文件--> # cat /etc/config/dhcp)

我正在尝试“漂亮打印”预订的 静态IP我通过 ssh 进入我的开放式路由器后的“设备”。 输入文件:

    config dnsmasq
        option domainneeded '0'
        option localise_queries '0'
        option rebind_protection '3'
        option rebind_localhost '5'
        option local '/lan/'
        option domain 'lan'
        option expandhosts '0'
        option authoritative '0'
        option readethers '0'
        option leasefile '/tmp/dhcp.leases'
        option resolvfile '/tmp/resolv.conf.auto'
        option localservice '2'
        option nonwildcard '0'
        list rebind_domain 'TC.direct'
        option confdir '/tmp/dnsmasq.d'
 
config dhcp 'lan'
        option interface 'lan'
        option leasetime '12h'
        option limit '60'
        option ra 'server'
        option dhcpv6 'server'
        option ra_management '1'
        option start '3'
 
config dhcp 'wan'
        option interface 'wan'
        option ignore '1'
 
config odhcpd 'odhcpd'
        option maindhcp '0'
        option leasefile '/tmp/hosts/odhcpd'
        option leasetrigger '/usr/sbin/odhcpd-update'
 
config host
        option name 'DEVICE 14'
        option dns '3'
        option mac 'eb:7f:29:10:19:14'
        option ip '192.168.1.14'
        option leasetime '12h'
 
config host
        option dns '3'
        option name 'DEVICE 11'
        option mac '3f:05:99:b9:73:11'
        option ip '192.168.1.11'
        option leasetime '12h'
 
config host
        option name 'DEVICE 54'
        option dns '3'
        option mac '37:10:81:f2:bb:54'
        option ip '192.168.1.54'
        option leasetime '12h'
 
config host
        option dns '3'
        option mac '62:f7:62:fc:75:55'
        option ip '192.168.1.55'
        option leasetime '12h'
        option name 'DEVICE 55'
 
config host
        option name 'DEVICE 08'
        option dns '3'
        option mac '45:70:a8:83:bd:08'
        option ip '192.168.1.8'
        option leasetime '12h'

期望的输出:(如果可能的话,按 IP 升序对结果进行排序“类似 -->| sort -t. -k1,1n -k2,2n -k3,3n -k4,4n -V"

device ip       device mac           device name
192.168.1.14    eb:7f:29:10:19:14    DEVICE 14
192.168.1.11    3f:05:99:b9:73:11    DEVICE 11
192.168.1.54    37:10:81:f2:bb:54    DEVICE 54
192.168.1.8     45:70:a8:83:bd:08    DEVICE 08

我尝试过awk '/option ip/ {printf $3; } /option mac/ {printf $3; } /option name/ { print $3 $4; }',但输入文件中的“数据块表单”不一致

答案1

文件sort.awk

function printentry(arg1, arg2, arg3){
  if (arg1!="") printf "%-15s   %-17s   %s\n", arg1, arg2, arg3
}
BEGIN{ 
  printentry("device ip", "device mac", "device name")
}
$0=="config host"{
  printentry(ip, mac, name)
  ip=mac=name=""
  next
}
$2=="ip"  { sub(/^[^\047]*/, ""); gsub(/\047/, ""); ip=$0;   next }
$2=="mac" { sub(/^[^\047]*/, ""); gsub(/\047/, ""); mac=$0;  next }
$2=="name"{ sub(/^[^\047]*/, ""); gsub(/\047/, ""); name=$0; next }
END{
  printentry(ip, mac, name)
}

输出:

$ awk -f sort.awk file
device ip         device mac          device name
192.168.1.14      eb:7f:29:10:19:14   DEVICE 14
192.168.1.11      3f:05:99:b9:73:11   DEVICE 11
192.168.1.54      37:10:81:f2:bb:54   DEVICE 54
192.168.1.55      62:f7:62:fc:75:55   DEVICE 55
192.168.1.8       45:70:a8:83:bd:08   DEVICE 08

或者按ip排序:

$ awk -f sort.awk file | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4
device ip         device mac          device name
192.168.1.8       45:70:a8:83:bd:08   DEVICE 08
192.168.1.11      3f:05:99:b9:73:11   DEVICE 11
192.168.1.14      eb:7f:29:10:19:14   DEVICE 14
192.168.1.54      37:10:81:f2:bb:54   DEVICE 54
192.168.1.55      62:f7:62:fc:75:55   DEVICE 55

相关内容