Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-20-2006, 12:02 PM   #1
LQ Newbie
Registered: Mar 2004
Posts: 25

Rep: Reputation: 15
Sed pattern matching

I need to write a script to delete a <VirtualHost>...</VirtualHost> entry of a particular domain from httpd.conf using sed or awq or anything.

The entry will be some what like this

DocumentRoot /www/docs/

How can i remove such an entry from httpd.conf using a bash script that will search and find the entry for and remove it.

I tried to do it with sed but the pattern was too complecated for me to understand and use. How do we match multiple line and remove those lines from a file using sed/awk. Plese help?
Old 08-20-2006, 01:37 PM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405

There probably several ways to tackle this. Probably? Yes, I don't know if there are any empty lines between the VirtualHost entries. I assume (dangerous, I know) that there are.

If so, here are 2 (one useful, one for fun):

Using awk:
awk 'BEGIN { FS="\n" ; RS="" } $3 !~ /ServerName { print $0, "\n" }' infile

Don't know the level of experience you have with awk, so here's a nutshell explanation.

BEGIN { FS="\n" ; RS="" } => This sets the Field (FS) and Record (RS) separator. Normally awk reads entries one line at the time, from left to right (RS="\n" and FS=" "), this makes sure that a one paragraph (empty line being the record separator) is read.

$3 !~ /ServerName { print $0, "\n" } => If field 3 does not match 'ServerName', print the paragraph.

If something isn't clear: Just ask.

And if you really, really want to do this with sed:

sed -n '
# if an empty line, check the paragraph
/^$/ b para
# else add it to the hold buffer
# at end of file, check paragraph
$ b para
# now branch to end of script
# this is where a paragraph is checked for the pattern
: para 
# return the entire paragraph
# into the pattern space
# look for the pattern, if there - delete, print all other paragraphs
' $1
Hope this gets you going again.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 08:30 PM
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 PM.

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