Using sed to remove lines from a config file

I have a script I am trying to run that will remove lines from an apache configuration file for a particular domain.  The lines are added via another script and it works fine and is added in the exact format I am trying to remove.  I must have the sed expression screwed up or something because it just prints the whole config file again without removing the lines I want.

The script is executed like so:
./script.sh domain.com aliasdomain.com

Here is the script:


#!/bin/sh

DOMAIN=$1;
ALIASDOMAIN=$2;


FQDOMAIN="www.$DOMAIN"
etc_conf_file=/tmp/httpd.include

echo -e "Removing http/web forwarding for $DOMAIN\n\n";

# Setup the /etc conf file
echo -e "Clearing the /etc config file.\n";
etc_conf="<VirtualHost x.x.x.x:80>\n\tServerName   $DOMAIN\n\tServerAlias  $FQDOMAIN\n\tRedirect Permanent / \"http://www.$ALIASDOMAIN/\"\n</VirtualHost>\n"
echo -e $etc_conf;

sed -e s_"$etc_conf"_""_g $etc_conf_file > $etc_conf_file.tmp




Any help would be much appreciated.

LVL 1
bdhtechnologyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dathhoCommented:
don't know if you lost someting in translation or not but it should be
sed -s s/"$etc_conf"/""/g $etc_conf_file > $etc_conf_file.tmp

not
sed -e s_"$etc_conf"_""_g $etc_conf_file > $etc_conf_file.tmp
 
0
bdhtechnologyAuthor Commented:
Actually you can use any char. you want for a delimiter.  Since the text I am replacing has a lot of slashes I used an underscore instead of escaping each slash.

0
nognewCommented:
Hello mate!
 I think your pattern does not match exactly the block it suppose to be matched. Try to debug your script in the folowing method:
Split the pattern into sections and see if it works. Maybe splitting is not aplicable in your particular case as the lines might be matched out of "Virtual Host" block, but that allows you to understand if your pattern works at all, and which bit is not working. See sample:
#!/bin/sh
 
DOMAIN=$1;
ALIASDOMAIN=$2;
 
 
FQDOMAIN="www.$DOMAIN"
etc_conf_file=/tmp/httpd.include
 
echo -e "Removing http/web forwarding for $DOMAIN\n\n";
 
# Setup the /etc conf file
echo -e "Clearing the /etc config file.\n";
etc_conf="<VirtualHost x.x.x.x:80>\n"
etc_conf1="\tServerName   $DOMAIN\n"
etc_conf2="\tServerAlias  $FQDOMAIN\n"
etc_conf3="\tRedirect Permanent / \"http://www.$ALIASDOMAI\"\n"
etc_conf4="</VirtualHost>\n"
echo -e $etc_conf;
echo -e $etc_conf1;
echo -e $etc_conf2;
echo -e $etc_conf3;
echo -e $etc_conf4;
 
cat $etc_conf_file | sed -e s_"$etc_conf"_""_g -e _"$etc_conf1"_""_g -e _"$etc_conf2"_""_g -e _"$etc_conf3"_""_g -e _"$etc_conf4"_""_g > $etc_conf_file.tmp

Open in new window

0
bdhtechnologyAuthor Commented:
nognew:
I tried your solution however that didn't seem to work either.  I guess my problem is trying to edit multiple lines with a line editor.  I ended up solving it by finding the starting and ending line numbers and then deleting them with sed like so:

DOMAIN=$1;
ALIASDOMAIN=$2;
 
 
FQDOMAIN="www.$DOMAIN"
etc_conf_file=/etc/apache2/httpd.include
 
echo -e "Removing http/web forwarding for $DOMAIN\n";
 
startline=`grep -n -B 2 -A 2 $DOMAIN $etc_conf_file | cut -s -d "-" -f1 | head -n1`;
endline=`grep -n -B 2 -A 2 $DOMAIN $etc_conf_file | cut -s -d "-" -f1 | tail -n1`;
 
sed -i -e "$startline,$endline d" $etc_conf_file

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.