clean /etc/hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting clean /etc/hosts
# 1  
Old 11-06-2006
clean /etc/hosts

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 Smilie)
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!

Last edited by DukeNuke2; 11-07-2006 at 04:17 AM..
# 2  
Old 11-06-2006
so, there are ways to do this in perl ( open the file for writing ) and replace the respective strings using regexp.
# 3  
Old 11-07-2006
What kind of cleaning do you want to do ?
Show us an example please.


Jean-Pierre.
# 4  
Old 11-07-2006
Quote:
Originally Posted by aigles
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)
# 5  
Old 11-07-2006
The following awk program clean the /etc/hosts file :
Code:
#! /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 :
Code:
#
# /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:
Code:
#
# /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.
# 6  
Old 11-07-2006
wow, fantastic work.... thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies

2. AIX

aix tcp wrappers hosts.allow hosts.deny?

hi all just installed the netsec.options.tcpwrapper from expansion pack, which used to be a rpm, for my aix 6.1 test box. it is so unpredictable. i set up the hosts.deny as suggested for all and allow the sshd for specific ip addresses/hostnames. the tcpdchk says the hosts allowed and... (0 Replies)
Discussion started by: wf201626
0 Replies

3. Shell Programming and Scripting

Clean up the space

Hi I just want to clean up the space in UNIX. Is there any special command that does this process or do I need to manually type the following rm <filename> Any help would be really appreciated. Thanks (2 Replies)
Discussion started by: bobby1015
2 Replies

4. Shell Programming and Scripting

How to clean this script?

Hello guys, this script partially works but it's still pretty ugly and, moreover, if the month is jan/feb/mar... it doesn't work at all. Could anyone say me how to correct, cut and clean a little bit? #!/usr/bin/ksh egrep -v -e "^\s*#" /file/permission | awk '{ print $1 }' | sort | uniq... (3 Replies)
Discussion started by: gogol_bordello
3 Replies

5. AIX

How to clean PV id?

When I run command: >chdev -l hdisk1 -a pv=clear It shows Method error (/etc/methods/chgdisk): 0514-062 Cannot perform the requested function because the specified device is busy. run: #>fuser -kxuc /dev/raw1 /dev/raw1: How to clean PV id? (4 Replies)
Discussion started by: rainbow_bean
4 Replies

6. UNIX for Dummies Questions & Answers

Help me clean this up

UNIX amature/novice. I've written a script (it works) that needs to be cleaned up. I'm searching for a way to perform a "go to , perform this then return" section of code. The books I have and searches i"ve done so far do not show me how to do this. I've pasted in the current code below. I've... (1 Reply)
Discussion started by: scanner248
1 Replies

7. UNIX for Dummies Questions & Answers

Hosts.allow and hosts.deny

Hello everyone, This is my first posts and I did search for a questions but did not find a question that answered my question unless of course I overlooked it. I'm running Solaris 8. I use ssh for the users but I have a user called "chatterbox" that uses telnet but I need for chatterbox to... (1 Reply)
Discussion started by: huddlestonsnk
1 Replies

8. UNIX for Advanced & Expert Users

Clean File

BeginDate 07/01/06 End: 07/31/06 Cust: A02991 - Burnham 0002000 5,829,773 145.3 0009701 4,043,850 267.3 2005000 286,785.13 100.0 BeginDate 07/01/06 End: 07/31/06 Cust: A01239 - East Track PSE Index A 0009902 317,356.82 890.2 0020021 ... (5 Replies)
Discussion started by: kris01752
5 Replies

9. UNIX for Dummies Questions & Answers

hosts.allow & hosts.deny

Hi! Im trying to use host.allow & host.deny to resrtic access to my sun machine, but it doesnt seem to work... I want to allow full access from certain IPīs (ssh,http,ftp,etc...) but deny all kind of conections from outsideworld, the way that im doing that is: hosts.allow ALL:127.0.0.1... (2 Replies)
Discussion started by: Sorrento
2 Replies
Login or Register to Ask a Question