How to remove spaces and newline in awk for string insertion?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to remove spaces and newline in awk for string insertion?



So, I have my apache2 config file like this:


<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>



What I want to do is insert a new BalancerMember with # before the line ProxySet lbmethod=byrequests. I will use shell script to do this.


ProxySet lbmethod=byrequests



So it should look like: #BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60


#BalancerMember "ajp://10.x.x.xxx:8009" route=node8 loadfactor=1 keepalive=on ttl=300 max=400 timeout=300 retry=60



What I have tried is:


awk -v s1='"' -v ip="10.0.7.1" -v no="8" '
/ProxySet lbmethod=byrequests/
print "ntt#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
}



So, the above solutions works fine, but leaves newlines, which I don't want. I have tried removing ORS.





Not sure what you mean by "is leaving newlines" but you ARE specifically printing newlines with print "n..." - is that the problem? If not then post the expected output and the output you're actually getting to clarify what the problem is.
– Ed Morton
Aug 10 at 3:58


print "n..."





Let me paste the output of my code for better understanding in my question.
– Ashutosh
Aug 10 at 4:03





@EdMorton @RavinderSingh13 This was my bad, the code works fine just needed to remove the n. Sorry for wasting your time guys. I feel so stupid now.
– Ashutosh
Aug 10 at 4:10


n




1 Answer
1



Could you please try following.(Creating variable line of awk which will have your new line's value in it)


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



Explanation: Adding explanation for above code here.


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.





I have edited my question code, because I'm using variables.
– Ashutosh
Aug 10 at 3:05





@Ashutosh your comments are coming across as extremely rude, entitled, and unappreciative. "change your answer according to my new code with variables.", and "Doesn't work, already tried this." and " Please see the code in the question again. I have already mentioned the same.". Seriously? You might want to consider that Ravinder is trying to help you AND is doing so for free.
– Ed Morton
Aug 10 at 3:42






@EdMorton, thank you for your support, Ashutosh- code seems to be working fine as per your question, be clear in your question else.
– RavinderSingh13
Aug 10 at 3:46





@EdMorton, sure done that now sir.
– RavinderSingh13
Aug 10 at 3:47





I didn't mean that. I am sorry for being rude.
– Ashutosh
Aug 10 at 3:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS