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

おすすめ記事