deleting lines above and below a word!
hi...
i jst want to delete a host entry from httpd.conf for eg: i have entries such as: <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik ServerName www.karthik.com </VirtualHost> <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik1 ServerName www.karthik1.com </VirtualHost> <VirtualHost 192.168.1.157:80> DocumentRoot /home/lalz ServerName lal.com </VirtualHost> i want to delete the entry containing the domain name lal.com ie..the output should be: <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik ServerName www.karthik.com </VirtualHost> <VirtualHost 192.168.1.157:80> DocumentRoot /home/karthik1 ServerName www.karthik1.com </VirtualHost> the entry for lal.com should be deleted!! anyone..plz help me out!! |
You could use
Code:
sed -i '/lal.com/d' httpd.conf Kind regards |
thankz for the reply..bt the command doesnt work!!
sed '/lal/d' httpd.conf only deletes that particular line... plz help me out!! |
Hi,
Using sed: Create a file with the following content: Code:
:t Run sed as follows: Code:
sed -f sedcmds infile Code:
$ cat infile EDIT: Or as a one-liner: Code:
sed ':t /<VirtualHost/,/VirtualHost>/ { /VirtualHost>/!{ $!{ N; bt } }; /lal.com/d; }' infile |
Rather than having all Apache virtual hosts configured in a single file, I recommend using instead
Code:
Include *.vhost However, your current problem can be easily solved via awk. It is not very elegant, but it should work well for you. It is not terribly well tested, but it worked flawlessly with the few VirtualHost configurations I tried it with. It modifies the specified configuration file, but saves the previous version with the suffix .saved. Code:
#!/bin/bash The awk script is a case insensitive line-based state machine. In the default state, it just outputs each input line as is. When the awk script encounters a line with the first token matching <VirtualHost, it changes state, and starts saving each input line into (a new) array vhost instead of outputting them. If the awk script encounters an input line with the first token matching ServerName, it checks if any of the specified server names are contained in the second token. If yes, this array is marked to be skipped. When the script encounters a line with the first token matching </VirtualHost>, it will reset to default mode. If the array was not marked skipped, all lines in the array are output at this point. There are a few minor tricks in there to make sure the script works with both gawk (GNU awk, my favourite) and mawk, but they're not very important unless you're very interested in awk scripting. Hope you find this useful. |
Hi,
this worked with your sample data Code:
sed ':a N; \@</VirtualHost>@ {/lal.com/ d;b}; ba' file |
Code:
$ awk 'BEGIN{ ORS=RS="</VirtualHost>"} /lal\.com/{next} RT{print $0} ' file or Ruby(1.9+) Code:
|
Hi crts,
I've done a lot with sed, but I don't recognize what this command does. Would you please explain the syntax? |
Quote:
I assume you do not recognize the non-standard delimiter in '\@</VirtualHost>@', right? This does the same as Code:
'/<\/VirtualHost>/' |
@crts: Nice short solution!!
BTW, the </ part isn't needed for the sample data, so this would also work: Code:
sed ':a N; /VirtualHost>/ {/lal.com/ d;b}; ba' file |
Yes, that's the first I've seen that the delimiter.
Also I'm guessing that the ":a N;" and "; ba" set up a loop in which the "{...}" command is run. Does it simply match from the first occurrence of "\@</VirtualHost>@" to the next occurrence? Or does it effectively create sections, and if "/lal.com/" is matched it deleted the section? |
Hi druuna,
you are right. I just had a look at your solution and noticed that you omit the '</' part. This spares us the obfuscating escape character. Examining your solution a bit further, you can transform it to Code:
sed ':t /VirtualHost>/! {N;bt}; /lal.com/ d' infile |
Quote:
|
More awk but kinda the same:
Code:
awk 'BEGIN{ORS=RS="</VirtualHost>\n"}!/lal\.com/ && RT' file |
Quote:
Also, if the file has an empty line at the end I get a duplicate output of '</VirtualHost>' in the last line. |
All times are GMT -5. The time now is 08:28 AM. |