Kodama's home / tips.

UNIX hosts ファイルから Mac 用 hosts への変換

#! /usr/bin/gawk -f
# K.Kodama
# convert UNIX-hosts-file to Mac-hosts-file.
BEGIN{ 
        print "; Macintosh hosts file";
        print strftime("; %Y-%m-%d.%H:%M:%S");
}
/^[0-9]/{
    ip=$1; host=$2;
    comment=$0;sub(/#.*/,"");sub($0,"",comment);
    if(comment~/^#/){ sub(/#/,"     ; ",comment); gsub(/[\t ]+/," ",comment);}
    if( host~/kobe-kosen\.ac\.jp/){
         fqdn=host
         print fqdn "    A       "ip comment ;
     }else{
         fqdn=host ".kobe-kosen.ac.jp"
         print fqdn "    A       "ip comment ;
         print host "    CNAME   " fqdn;
     }
     for(i=3;i<=NF;i++){     print $i "      CNAME   " fqdn;}
     print ""
     next
}
{sub(/#/,";"); print $0; nenxt}

Kodama's home / tips.