通过IP地址解析文件 - bash

时间:2015-03-29 18:29:42

标签: bash shell awk sed grep

我正在尝试配置MapR群集:

./configure.sh -C 10.60.63.6, 10.226.86.24, 10.144.64.75 -Z 10.226.86.24 -N mycluster —create-user

IP地址来自/etc/hosts

    10.60.63.6      dgnode-1 dgnode-1.dg.local
    10.144.64.75    dgnode-2 dgnode-2.dg.local
    10.226.98.24    namenode namenode.dg.local

我需要的是一个bash脚本,它解析该文件以生成如上所述的命令。我不是很擅长bash,我怎么能做到这一点? 模式是这样的:

/opt/mapr/server/configure.sh -C <all_nodes> -Z <namenode> -HS <namenode> -N MyCluster.

以下是/etc/hosts的示例:         root @ ip-10-226-98-24:/ opt / mapr / server #cat / etc / hosts         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
    10.60.63.6  dgnode-1 dgnode-1.dg.local
    10.144.64.75    dgnode-2 dgnode-2.dg.local
    10.226.98.24    namenode namenode.dg.local
    ## vagrant-hostmanager-end

1 个答案:

答案 0 :(得分:0)

使用此awk:

awk '/## vagrant-hostmanager-start/{
     a=1;
     next
  }
  /## vagrant-hostmanager-end/{
     exit
  }
  !a{
    next
  }
  a==1{
     a++;
     ips=$1;
     next
  }
  a>1{
     ips=ips ", " $1
  }
  $2=="namenode"{
     nn=$1
  } 
  END {
    printf "/opt/mapr/server/configure.sh -C %s -Z %s -HS %s -N MyCluster\n",
            ips, nn, nn
  }' /etc/hosts
  

/opt/mapr/server/configure.sh -C 10.60.63.6, 10.144.64.75, 10.226.98.24 -Z 10.226.98.24 -HS 10.226.98.24 -N MyCluster