Operating System - HP-UX
1824169 Members
3255 Online
109669 Solutions
New Discussion юеВ

Howto merge 2 differrent /etc/hosts files?

 
SOLVED
Go to solution
PSS SYS ADMIN
Super Advisor

Howto merge 2 differrent /etc/hosts files?

Hi everyone,
I want to merge the /etc/hosts files caoming from 2 different hosts.
How I can do it?

Regards...
PSS
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: Howto merge 2 differrent /etc/hosts files?

PSS,

Have a look at the sort man page, particularly the merge section. That should get you started.


Pete

Pete
Steven E. Protter
Exalted Contributor

Re: Howto merge 2 differrent /etc/hosts files?

In a third file:

I'd cp /etc/hosts > temp
then the second

cp /etc/hosts >> temp

Append mode.

I might try sort, but in the end I'm going to have to go through the files manually and look for conflicts.

For sure, get rid of the second loopback line.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Uday_S_Ankolekar
Honored Contributor

Re: Howto merge 2 differrent /etc/hosts files?

cat hostfile1 hostfile2 >newhostfile.

-USA..
Good Luck..
Mark Greene_1
Honored Contributor

Re: Howto merge 2 differrent /etc/hosts files?

I would try this:

- get both host files on the same system. Call them host1 and host2

- merge them like this:

cat host1> tmphost
cat host2>> tmphost

- visually verify the merge:

pg tmphost

- filter the merged file:

cat tmphost|sort|uniq >tmp2host

- visually verify the new file:

pg tmp2host

Now it should be a simple job of removing any remaining duplicates using vi. When done you can copy the file to /etc/hosts and HUP the appropriate services or reboot.

HTH
mark
the future will be a lot like now, only later
Stefan Farrelly
Honored Contributor
Solution

Re: Howto merge 2 differrent /etc/hosts files?

I would cat the 2 files together into 1 file;

cat /etc/hosts >/tmp/newhosts
cat >>/tmp/newhosts

then;

cat /tmp/newhosts | sort -u > /etc/hosts

(Make a backup of /etc/hosts first just in case). The sort -u means remove all duplicates. Then check the hostname for your server has the correct IP in your new hosts file and that should do it.

Im from Palmerston North, New Zealand, but somehow ended up in London...
doug mielke
Respected Contributor

Re: Howto merge 2 differrent /etc/hosts files?

I'd do the cat file1 file2 >> file3

Don't worry too much about the possible duplicates, but be aware that only the 1st one will be read. Also, a small performance gain is possible if you have the most often used entries at the top.
Kent Ostby
Honored Contributor

Re: Howto merge 2 differrent /etc/hosts files?

PSS --

I'm assuming you want to remove duplicate items from the two files.

Part of it depends on if you want to preserve comments beyond the top of the files.

If you dont then you can run the attached script as follows:

combine input1 input2 output

It will keep the comments from the top of the FIRST input file specified. The final assumption is that you dont have files that start with ".useme" in the directory where you are running the script.

Best regards,

Kent M. Ostby


"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Rodney Hills
Honored Contributor

Re: Howto merge 2 differrent /etc/hosts files?

You could use perl to merge and remove duplicates.

while(<>) {
next if /^#/ or /^$/;
@a=split('\s+',$_);
$hold{$a[0]}{$a[$i]}=1 for $i (1..$#a);
}
foreach $ip (sort keys %hold) {
print "$ip\t",join(" ",keys %{$hold{$ip}}),"\n";
}

perl aboveprogram hostfile1 hostfile2 >newhostfile

HTH

-- Rod Hills
There be dragons...
Rory R Hammond
Trusted Contributor

Re: Howto merge 2 differrent /etc/hosts files?

(my prefrence is)

1. grep "^#" /etc/hosts > newhosts
2. grep -v "^#" /tmp/hosts /etc/hosts|sort|uniq >> newhosts

3. vi newhosts
put localhost below comments.
put system below localhost.
fix problem childern like if host are not duplicate host names and ip address that did compare from the sort uniq.
4. cp /etc/hosts /etc/hosts.rl
5. cp newhosts /etc/hosts

Rory
There are a 100 ways to do things and 97 of them are right