如何正确格式化主机文件 - bash

时间:2014-05-01 14:31:46

标签: bash unix hadoop sed

我正在尝试创建一个Hadoop群集,为了做到这一点,我必须将/etc/hosts文件格式化为只包含群集中节点的IP地址。目前它看起来像这样:

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

最后,我只想将其保留为

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

如何使用sedgrepcat或任何Bash命令实现这一目标?

1 个答案:

答案 0 :(得分:2)

只需使用这样的sed:

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