1

I have a configuration that is disabled because its embedded in a comment. My Sed is deleting the line beneath my pattern, but the line with the pattern gets deleted too. Can I tell Sed to keep the current line? Or just delete the line before and after the found pattern.

Input

... <!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> --> ... 

Sed

# sed -e '/<Connector port="8009" protocol="AJP\/1.3" redirectPort="8443" \/>/,+2d' server.xml 

Output

... <!-- ... 

Wish to be Output:

... <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> ... 
1
  • 2
    Your config seems to be an XML document. Use a proper XML parser to process it. Commented Jun 5, 2014 at 13:09

2 Answers 2

3

Your input seems to be an XML document. Use a proper parser to process it. For example, xsh:

open server.xml ; for my $c in //comment()[contains(.,'<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />')] xinsert chunk $c replace $c ; save :b ; 
Sign up to request clarification or add additional context in comments.

3 Comments

I have no idea if your snippet is correct, but this is the correct approach. +1
@DanFego: Thanks. I tested it on a server.xml I created myself, as no sample input file was given. :-)
Using the right tool for the right job is hardly one ever wants. :P.
1

This might work for you (GNU sed):

sed '$!N;/\n<Connector port="8009" protocol="AJP\/1.3" redirectPort="8443" \/>/{s/.*\n//p;$!N;d};P;D' file 

Keep 2 lines in the pattern space and delete the first, print the next and delete the following line if the pattern is found.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.