[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

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.

0
bdhtechnology
Asked:
bdhtechnology
  • 2
1 Solution
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now