【问题标题】:How to remove spaces and newline in awk for string insertion?如何删除 awk 中的空格和换行符以进行字符串插入?
【发布时间】:2019-01-17 13:52:47
【问题描述】:

所以,我的 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 行之前插入一个带有# 的新BalancerMember。我将使用 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。

【问题讨论】:

  • 不确定您所说的“正在离开换行符”是什么意思,但您专门用print "\n..." 打印换行符 - 是这个问题吗?如果不是,请发布预期的输出和您实际得到的输出以澄清问题所在。
  • 让我粘贴我的代码输出以便更好地理解我的问题。
  • @EdMorton @RavinderSingh13 这是我的错,代码工作正常,只需要删除\n。对不起,浪费你们的时间了伙计们。我现在觉得自己好傻。

标签: string bash shell awk newline


【解决方案1】:

您能否尝试以下操作。(创建变量 lineawk 将包含您的新行的值)

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.

【讨论】:

  • 我已经编辑了我的问题代码,因为我正在使用变量。
  • @Ashutosh,抱歉,我没听明白,请告诉我什么是行不通的?
  • @Ashutosh 你的 cmets 给人的印象是非常粗鲁、有权利和不欣赏。 “根据我的带有变量的新代码更改您的答案。”和“不起作用,已经尝试过了。”和“请再次查看问题中的代码。我已经提到过。”。严重地?您可能想考虑一下,Ravinder 正在努力帮助您,而且是免费的。
  • @EdMorton,感谢您的支持,Ashutosh- 根据您的问题,代码似乎运行良好,请在其他问题中明确说明。
  • 我不是那个意思。很抱歉我的粗鲁。
猜你喜欢
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 2016-09-16
  • 2011-08-27
相关资源
最近更新 更多