【问题标题】:Parsing a file by the IP address - bash通过 IP 地址解析文件 - bash
【发布时间】:2015-06-02 16:40:08
【问题描述】:

我正在尝试配置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 本地主机

    # 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

【问题讨论】:

    标签: bash shell awk sed grep


    【解决方案1】:

    使用这个 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

    【讨论】:

    • 之后我想执行那个命令……我该怎么做?
    • 我没做好:root@ip-10-226-98-24:/opt/mapr/server# awk 'NR==1{ips=$1} NR&gt;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 127.0.0.1, , #, ::1, fe00::0, ff00::0, ff02::1, ff02::2, ff02::3, , ##, 10.60.63.6, 10.144.64.75, 10.226.98.24, ##, -Z 10.226.98.24 -HS 10.226.98.24 -N MyCluster
    • 这适用于在 ## vagrant-hostmanager-start## vagrant-hostmanager-end 块之间包含行的原始主机文件。
    • 现在检查我更新的答案以获取有问题的新主机文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2022-12-02
    • 1970-01-01
    • 2015-09-14
    • 2020-08-06
    • 2014-10-11
    相关资源
    最近更新 更多