SED, match a line block


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED, match a line block
# 1  
Old 05-17-2010
SED, match a line block

Hello,
I want to do a simple substitution using sed but I can't find a solution. Basically, from a Apache conf file, I would like to remove everything included between the <VirtualHost> and </VirtualHost> e.g
Code:
SSLMutex  file:/var/run/ssl_mutex
<VirtualHost _default_:443>
# A lot of config that I don't want
<VirtualHost>
#another config

So the result I want is everything withtout the VirtualHost part:
Code:
SSLMutex  file:/var/run/ssl_mutex
#another config

I tried:
Code:
gsed -e "s#<VirtualHost.*</VirtualHost>##g" /tmp/test

but it does not match the line block Smilie

Any ideas ?

R.
# 2  
Old 05-17-2010
Code:
 
sed '/<VirtualHost/d' input_file

# 3  
Old 05-17-2010
Try with awk...

Code:
awk '/<VirtualHost/,/VirtualHost>/{next}1' infile

Or sed...
Code:
sed '/<VirtualHost/,/VirtualHost>/d' infile

# 4  
Old 05-17-2010
try this

Code:
sed -e 's/\<Virtualhost[^$]+\<\/VirtualHost\>//g' /tmp/test

Moderator's Comments:
Mod Comment Use code tags please, ty.
# 5  
Old 05-17-2010
ok ! Thanks for the fast answer.
# 6  
Old 05-17-2010
MySQL

Quote:
Originally Posted by panyam
Code:
 
sed '/<VirtualHost/d' input_file

only this code doesnt work..
let try this like

Code:
sed -e '/<VirtualHost/d' -e '/^\# A/d' myfile



Quote:
Originally Posted by m_kapur83
try this

Code:
sed -e 's/\<Virtualhost[^$]+\<\/VirtualHost\>//g' /tmp/test

this code doesnt work..

+ isnt unnecessary..this character doesnt merge multilines in sed..
for multi line edit use N Smilie

or with + use extended regular expression with r


Code:
sed -r '/<VirtualHost._+/d' test | sed -r '/<VirtualHost>.?/d' | sed -e '/# A/d'
SSLMutex  file:/var/run/ssl_mutex
#another config


Posix basic regular expression syntax without -r option (instead of use extended regular expressions)

Code:
sed -e '/<VirtualHost.\+/d' -e '/<VirtualHost>\?/d' test | sed -e '/# A/d'
SSLMutex  file:/var/run/ssl_mutex
#another config

Regards
ygemici

Last edited by ygemici; 05-17-2010 at 11:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

4. Shell Programming and Scripting

Using sed to pattern match within a particular multiline block and take action

Hi all, This is my first post, so please go easy if I broke some rules. Not accustomed to posting in forums... :) I'm looking for help on pattern matching within a multiline block and looking to highlight blocks/block-ids that do NOT contain a particular pattern. For example an input file... (5 Replies)
Discussion started by: tirodad
5 Replies

5. Shell Programming and Scripting

SED - Match a line from a File

Hi, I want to match a line which exists in a file. I have written a test script similar to below - The content of the file file.txt would be like this - /usr/bin/1234.xcf /usr/bin/3456.xcf /usr/bin/7897.xcf /usr/bin/2345.xcf out=`sed -n '\/usr\/bin\/7897.xcf/p' file.txt 2>&1`... (3 Replies)
Discussion started by: angshuman_ag
3 Replies

6. Shell Programming and Scripting

Sed scripting, match text within line and replace

New to sed... Have a file foo.txt (below). Need to replace text on 2 lines, but can only feed sed the first few characters of each line (all lines are unique). So, in my example, I have put '$' in place of what I need to figure out how to feed the whole line. What I have thus far: sed -e... (6 Replies)
Discussion started by: boolean2222
6 Replies

7. Shell Programming and Scripting

SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail MY INPUT DATA: CURRENT CODE I'M USING: sed -n '/*$/,/;/p' $INPUT_FILE RESULTS I'M GETTING: RESULT I looking to... (5 Replies)
Discussion started by: danmauer
5 Replies

8. Shell Programming and Scripting

sed: find match and delete the line above

I am searching a dhcpd.conf to find the hardware ethernet match, then once the match is found delete just the line above it. For example: testmachine.example { hardware ethernet 00:00:00:00:00:00; fixed address 192.168.1.100; next-server 192.168.1.101; filename "linux-install/pxelinux.0"; }... (3 Replies)
Discussion started by: cstovall
3 Replies

9. Shell Programming and Scripting

Multiple line match using sed

Please help! Input pattern, where ... could be any number of lines struct A { Blah1 Blah2 Blah3 ... } B; output pattern struct AB { Blah1 Blah2 Blah3 ... }; I need help in extracting everything between { and } if it would have been on a single line { \(.*\)} should have worked. (15 Replies)
Discussion started by: SiftinDotCom
15 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question