LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2014, 05:30 AM   #1
nidhintomson
LQ Newbie
 
Registered: Sep 2005
Location: Bangalore
Posts: 6

Rep: Reputation: 0
Request for bash Script to comment Paragraph with a particular occourance.


Hello All,

I am searching for a script which will comment out a paragraph with a pattern string occurrence.
Here is the part of the file.

<VirtualHost *:80>
ServerAdmin <some email id>
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin <some email id>
DocumentRoot /rhel_images/rhel59
ServerName yumrepo.clusterenv.com
ErrorLog logs/yumrepo.clusterenv.com.com-error_log
CustomLog logs/yumrepo.clusterenv.com-access_log common
</VirtualHost>
[root@node1 ~]#




For example:-

If I give "yumrepo" as the pattern, the script should comment out all lines starting from

<VirtualHost *:80>
ServerAdmin <some email id>
DocumentRoot /rhel_images/rhel59
ServerName yumrepo.clusterenv.com
ErrorLog logs/yumrepo.clusterenv.com.com-error_log
CustomLog logs/yumrepo.clusterenv.com-access_log common
</VirtualHost>


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.


Looking forward to hear from you,

Thanks and Regards
Nidhin Tomson
 
Old 09-06-2014, 05:55 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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:

BASH intros:
http://www.gnu.org/software/bash/man...ode/index.html
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

BASH scripting guides:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
# these are must reads so do read them!

Common questions / problems:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.orgDontReadLinesWithFor
http://mywiki.wooledge.org/UsingFind
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Also see:
http://www.linuxquestions.org/questi...l-links-35334/

The Advanced BASH scripting guide:
http://www.tldp.org/LDP/abs/html/

Bourne shell (compatibility):
http://www.grymoire.com/Unix/Sh.html
 
Old 09-06-2014, 06:09 AM   #3
nidhintomson
LQ Newbie
 
Registered: Sep 2005
Location: Bangalore
Posts: 6

Original Poster
Rep: Reputation: 0
This is what I have tried for:-
sed -e '/./{H;$!d;}' -e 'x;/yumrepo/b' -e d /etc/httpd/conf/httpd.conf|awk '{if (NR!=1) {print}}'

And the output is:

<VirtualHost *:80>
ServerAdmin <some email id>
DocumentRoot /rhel_images/rhel59
ServerName yumrepo.clusterenv.com
ErrorLog logs/yumrepo.clusterenv.com.com-error_log
CustomLog logs/yumrepo.clusterenv.com-access_log common
</VirtualHost>


Now my requirement is I need to comment the same output to the parent file (I mean the source file)



Looking forward to hear from you

Thanks and Regards
Nidhin Tomson
 
Old 09-06-2014, 06:40 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You mean
Code:
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.)

Last edited by unSpawn; 09-06-2014 at 06:42 AM.
 
Old 09-07-2014, 05:24 AM   #5
nidhintomson
LQ Newbie
 
Registered: Sep 2005
Location: Bangalore
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
You mean
Code:
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.

Looking forward to hear from you,

Thanks and Regards
Nidhin Tomson
 
Old 09-07-2014, 07:07 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by nidhintomson View Post
As an addition, instead of just printing, I need to actually insert the pound sign to the source file.
So how would you propose you do that?
 
Old 09-07-2014, 08:17 AM   #7
nidhintomson
LQ Newbie
 
Registered: Sep 2005
Location: Bangalore
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
So how would you propose you do that?
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
 
Old 09-07-2014, 08:26 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
See post #4, second command?
 
Old 09-07-2014, 09:48 AM   #9
nidhintomson
LQ Newbie
 
Registered: Sep 2005
Location: Bangalore
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
See post #4, second command?
Second command will do good if it can comment a paragraph, rather than by number of lines. How do we rephrase the command to achieve that ?
 
Old 09-07-2014, 10:26 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 09-07-2014, 11:42 AM   #11
nidhintomson
LQ Newbie
 
Registered: Sep 2005
Location: Bangalore
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
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 ?
 
Old 09-10-2014, 03:12 PM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Awk doesn't, sed does.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Request for comment - How to improve TeX in Slackware? allend Slackware 28 04-18-2013 06:55 PM
[SOLVED] Script to get the word count of a paragraph from a long message lakshminarayanan Linux - Newbie 7 11-10-2011 01:07 AM
[SOLVED] Bash Scripting Error: EOF encountered in a comment? dilzniksan Programming 4 06-02-2010 11:55 AM
Comment blocks in Bash? spiffytech Linux - Software 3 07-28-2006 10:59 AM
Request For Comment: xfree86 4.4 carboncopy Slackware 4 03-06-2004 09:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration