代码之家  ›  专栏  ›  技术社区  ›  Ashutosh

如何删除awk中的空格和换行符以插入字符串?

  •  -1
  • Ashutosh  · 技术社区  · 6 年前

    所以,我的apache2配置文件如下:

    <Proxy balancer://mycluster>
                 BalancerMember "ajp://10.x.x.xxx:8009" route=node1 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
                 BalancerMember "ajp://10.x.x.xx:8009" route=node2 loadfactor=1 keepalive=on  ttl=300 max=400 timeout=300 retry=60
                 BalancerMember "ajp://10.x.x.xxx:8009" route=node3 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
                 BalancerMember "ajp://10.x.x.xx:8009" route=node4 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
                 BalancerMember "ajp://10.x.x.xx:8009" route=node5 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
                 BalancerMember "ajp://10.x.x.xx:8009" route=node6 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
                 BalancerMember "ajp://10.x.x.xxx:8009" route=node7 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60
                ProxySet lbmethod=byrequests
        </Proxy>
    

    我要做的是在行前插入一个带有#的新平衡器成员 ProxySet lbmethod=byrequests . 我将使用shell脚本来执行此操作。

    所以看起来应该是: #BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60

    我试过的是:

    awk -v s1='"' -v ip="10.0.7.1" -v no="8" '
    /ProxySet lbmethod=byrequests/{
    print "\n\t\t#BalancerMember " s1 "ajp://" ip ":8009" s1 " route=node" no " loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60"
    next
    }
    1' /tmp/000-site.conf > /tmp/000-site.conf.tmp && mv /tmp/000-site.conf.tmp /tmp/000-site.conf
    }
    

    所以,上面的解决方案很好,但是留下了新行,这是我不想要的。我试过移除ORS。

    1 回复  |  直到 6 年前
        1
  •  2
  •   RavinderSingh13    6 年前

    请尝试以下操作。(正在创建变量 line 属于 awk 这将有你的新产品的价值)

    awk -v line='#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60' '
    /ProxySet lbmethod=byrequests/{
      print "             " line ORS $0
      next
    }
    1'  Input_file
    

    说明: 在此处添加上述代码的说明。

    awk -v line='#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60' ' ##Creating variable line.
    /ProxySet lbmethod=byrequests/{     ##Checking condition here if a line has string ProxySet lbmethod=byrequests then do following.
      print "             " line ORS $0 ##Printing space then variable line ORS and then print currrent line value then.
      next                              ##Mentioning next out of the box keyword to skip all further statements.
    }
    1                                   ##Mentioning 1 will print the lines here, awk works on condition then action, making condition true here, print action will happen.
    '  Input_file                       ##Mentioning Input_file name here.