LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 08-20-2006, 01:02 PM   #1
digitalbrutus
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

<VirtualHost 198.168.1.7>
ServerAlias something.com
ServerName www.something.com
DocumentRoot /www/docs/
</VirtualHost>

How can i remove such an entry from httpd.conf using a bash script that will search and find the entry for www.something.com 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, 02:37 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

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 www.something.com/ { 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 www.something.com/ { print $0, "\n" } => If field 3 does not match 'ServerName www.something.com', print the paragraph.

If something isn't clear: Just ask.

And if you really, really want to do this with sed:
Code:
#!/bin/bash

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


Reply


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


All times are GMT -5. The time now is 05:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration