パターンがある行の末尾にパターンと結合されたいくつかの文字列を追加する方法

パターンがある行の末尾にパターンと結合されたいくつかの文字列を追加する方法

/etc/hostsファイルのパターンに一致する各行の末尾に.comを追加したいと思います。

サンプルファイルの内容:

127.0.0.1   localhost
1.2.3.4 hostname1 hostname1.xyz hostname1.xyz.  
1.2.3.5 hostname2 hostname2.xyz hostname2.xyz.  
1.2.3.6 hostname3 hostname3.xyz hostname3.xyz.  

私はそれが次のように見えるようにしたいです:

127.0.0.1   localhost localhost.com
1.2.3.4 hostname1 hostname1.xyz hostname1.xyz. hostname1.xyz.com  
1.2.3.5 hostname2 hostname2.xyz hostname2.xyz. hostname2.xyz.com  
1.2.3.6 hostname3 hostname3.xyz hostname3.xyz. hostname3.xyz.com

この効果を得るためのsedコマンドはありますか?awk

ベストアンサー1

そしてawk

$ awk '$0 = $0 " " $NF ($NF ~ /\.$/ ? "" : ".") "com"' <file
127.0.0.1   localhost localhost.com
1.2.3.4 hostname1 hostname1.xyz hostname1.xyz. hostname1.xyz.com
1.2.3.5 hostname2 hostname2.xyz hostname2.xyz. hostname2.xyz.com
1.2.3.6 hostname3 hostname3.xyz hostname3.xyz. hostname3.xyz.com

おすすめ記事