cybertextron
cybertextron

Reputation: 10961

how to correctly format a hosts file - bash

I'm trying to create a Hadoop cluster, and in order to do so, I have to format my /etc/hosts file to just contain the ip addresses of the nodes in the cluster. Currently it looks like this:

127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

## vagrant-hostmanager-start
<ipaddress1>   namenode
<ipaddress2>  secondarynamenode
<ipaddress3>  slave1
<ipaddress4>   slave2
## vagrant-hostmanager-end

At the end, I just want to leave it as

## vagrant-hostmanager-start
<ipaddress1>   namenode
<ipaddress2>  secondarynamenode
<ipaddress3>  slave1
<ipaddress4>   slave2
## vagrant-hostmanager-end

How can accomplish that using sed, grep, cat or any Bash command?

Upvotes: 1

Views: 198

Answers (1)

anubhava
anubhava

Reputation: 784878

Just use sed like this:

sed -n '/## vagrant-hostmanager-start/,/## vagrant-hostmanager-end/p' /etc/hosts
## vagrant-hostmanager-start
<ipaddress1>   namenode
<ipaddress2>  secondarynamenode
<ipaddress3>  slave1
<ipaddress4>   slave2
## vagrant-hostmanager-end

Upvotes: 2

Related Questions