Request for bash Script to comment Paragraph with a particular occourance.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The script should make sure that it don't comment out other occurrences of VirtualHost ServerAdmin,DocumentRoot,ServerName ErrorLog CustomLog. Since this file has multiple entries for the same.
You are a post Graduate in Computer Application.
You are currently working as a system Administrator.
Those two combined should have led at least to a fleeting understanding of available command line tools and rudimentary shell scripting skills.
So please start by posting what you've tried and where you failed.
Else here's some references to help get you started:
sed -e '/./{H;$!d;}' -e 'x;/yumrepo/b' -e d /etc/httpd/conf/httpd.conf|awk '{if (NR!=1) {print "#",$ALL}}'
?
Or
Code:
sed -e '/rhel59/,+4 s/^/#/' -i /etc/httpd/conf/httpd.conf
? (Sorry, can't get this anchored right.)
Thankyou Spawn, We are getting close to our requirement.
I would prefer the First code [sed -e '/./{H;$!d;}' -e 'x;/yumrepo/b' -e d /etc/httpd/conf/httpd.conf|awk '{if (NR!=1) {print "#",$ALL}}'] . As an addition, instead of just printing, I need to actually insert the pound sign to the source file.
Instead of making AWK to print the output, can we have it insert the # ? (Something like I as a switch for sed to insert ?) Not sure though about the syntax and possibility.
Last edited by nidhintomson; 09-07-2014 at 08:21 AM.
Reason: More clarity
Problem is your "anchor" is somewhere in the middle of the vhost container and you're using tools that don't know that the "container" concept. So what you have to do is to detect the start of the container, store any subsequent lines, detect the search string, act on that, detect the end of the container and if the search string was detected then act on it. How you do that depends on what you're comfortable working with. I'd prolly script it.
*And having separate vhost files obviously makes management way easier.
Problem is your "anchor" is somewhere in the middle of the vhost container and you're using tools that don't know that the "container" concept. So what you have to do is to detect the start of the container, store any subsequent lines, detect the search string, act on that, detect the end of the container and if the search string was detected then act on it. How you do that depends on what you're comfortable working with. I'd prolly script it.
*And having separate vhost files obviously makes management way easier.
So how about with our first code ? Can we rephrase it to insert pound on the beginning of all lines it prints as output ? Some kind of switch for AWK, to insert the character rather than just printing it out ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.