awkが文字列を別の行に連結するのはなぜですか?

awkが文字列を別の行に連結するのはなぜですか?

さようなら行ってください。

/etc/resolv.conf.dnsphから抽出されたIPv6アドレスからping6を実行しようとしています。しかし、awkはIPv6アドレスを1行にまとめたようです。

$ grep ^nameserver /etc/resolv.conf.dnsph |awk '{print $2}'
2001:1890:1001:2224::1
2001:1890:1001:2424::1
$ ping6 $(grep ^nameserver /etc/resolv.conf.dnsph |awk '{print $2}')
ping6: Source routing is deprecated by RFC5095.
Usage: ping6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
             [-l preload] [-m mark] [-M pmtudisc_option]
             [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
             [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
             [-W timeout] destination
$ echo $(grep ^nameserver /etc/resolv.conf.dnsph |awk '{print $2}')
2001:1890:1001:2224::1 2001:1890:1001:2424::1

「END {printf "\n"}" を追加しても違いはありません。

$ ping6 $(awk '/^nameserver/ {print $2}; END {printf "\n"}' /etc/resolv.conf.dnsph)
ping6: Source routing is deprecated by RFC5095.
Usage: ping6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
             [-l preload] [-m mark] [-M pmtudisc_option]
             [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
             [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
             [-W timeout] destination
$ echo $(awk '/^nameserver/ {print $2}; END {printf "\n"}' /etc/resolv.conf.dnsph)
2001:1890:1001:2224::1 2001:1890:1001:2424::1

アドバイスしてください。

ビヨルン

ベストアンサー1

複数のアドレスが与えられたら、宛先ping6(最後のアドレス)に到達するためのホップとして解釈します。あなたの場合、出力を繰り返す必要があります。

for ip in $(awk '/^nameserver/ { print $2 }' /etc/resolv.conf.dnsph); do
    ping6 "$ip"
done

AWK は、最初のコマンドに示すように、各 IP アドレスを 1 行に正しく出力しますが、引用符は使用しません。コマンドの置き換え改行文字が効果的に削除されます。これは単語間の区切り文字として機能します。

おすすめ記事