hi all,
i need to write a script. it should be able to clean the /etc/hosts file like this:
first field is the ip (what else ;))
second field is the fqdn (full qualified domain name)
third field is the hostname
the fourth to n'th field is the rest.
i hope you understand what is needed?
tia,
DN2
btw. i use solaris to run the script. so there is no gawk only (n)awk!
so, there are ways to do this in perl ( open the file for writing ) and replace the respective strings using regexp.
aigles
3
What kind of cleaning do you want to do ?
Show us an example please.
Jean-Pierre.
o.k.:
the file looks like this:
192.168.0.100 hostname alias anotheralias hostname.mydomain.com
and should look like this
192.168.0.100 hostname.mydomain.com hostname alias anotheralias (and so on for every other alias)
aigles
5
The following awk program clean the /etc/hosts file :
#! /usr/bin/awk -f
NF>0 && $1 !~ /^#/ {
comment = "";
ip = $1;
host_alias = "";
fqdn = "";
if (match($0, /#/)) {
comment = substr($0, RSTART);
$0 = substr($0, 1, RSTART-1);
}
for (i=2; i<=NF; i++) {
if ($i ~ /.*\..*\..*/)
fqdn = (fqdn ? fqdn OFS : "") $i;
else
host_alias = (host_alias ? host_alias OFS : "") $i;
}
print ip, fqdn, host_alias, comment;
next;
}
1
Input file :
#
# /etc/host
#
192.168.0.100 host0 alias0a alias0b host0.mydomain.com
192.168.0.101 host1 alias1a alias1b host1.mydomain.com # Comment for host1
192.168.0.102 host2.mydomain.com host2 alias2
192.168.0.103 host3.mydomain.com host3a hostb host3b.mydomain.com
192.168.0.104 host4
192.168.0.105 host5.mydomain.com
Output:
#
# /etc/host
#
192.168.0.100 host0.mydomain.com host0 alias0a alias0b
192.168.0.101 host1.mydomain.com host1 alias1a alias1b # Comment for host1
192.168.0.102 host2.mydomain.com host2 alias2
192.168.0.103 host3.mydomain.com host3b.mydomain.com host3a hostb
192.168.0.104 host4
192.168.0.105 host5.mydomain.com
Jean-Pierre.
wow, fantastic work.... thank you!