Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Imagine I have a file like this:

INSERT INTO table VALUES('1','<p><em>The lazy fox jumps again</em></p>bunch of other html<p><em>Is the lazy fox crazy?</em></p>')
INSERT INTO table VALUES('2','<p><em>The lazy fox jumps again</em></p>bunch of other html<p><em>Is the lazy fox crazy?</em></p>')
INSERT INTO table VALUES('3','<p><em>The lazy fox jumps again</em></p>bunch of other html<p><em>Is the lazy fox crazy?</em></p>')

And I wish to delete only the first occurance of <p><em> and </em></p> so I end up with something like this:

INSERT INTO table VALUES('1','The lazy fox jumps againbunch of other html<p><em>Is the lazy fox crazy?</em></p>')
INSERT INTO table VALUES('2','The lazy fox jumps againbunch of other html<p><em>Is the lazy fox crazy?</em></p>')
INSERT INTO table VALUES('3','The lazy fox jumps againbunch of other html<p><em>Is the lazy fox crazy?</em></p>')

... how can I do that with sed (or perl)? The statement...:

sed "1,/INSERT INTO/s/<p><em>//g"

... only replaces the first occurance in the file, not on every line.

Help is much appreciated.

share|improve this question
add comment

2 Answers

If you want to process all lines with INSERT INTO, do not provide address range. If you want to only replace the first occurence of a string, do not provide /g:

sed -e '/INSERT INTO/s/<p><em>//' -e '/INSERT INTO/s/<\/em><\/p>//' 
share|improve this answer
    
I edited your answer to include the second substitution the OP asked or. Hope you don't mind. –  terdon Sep 17 '12 at 10:45
    
@terdon: Thanks. –  choroba Sep 17 '12 at 10:46
add comment

Here's one way you could do it with perl:

perl -pe 's:<p><em>(.*?)</em></p>:$1:' infile

The .*? quantifier is non-greedy, so only the first pair of tags will be matched.

Output:

INSERT INTO table VALUES('1','The lazy fox jumps againbunch of other html<p><em>Is the lazy fox crazy?</em></p>')
INSERT INTO table VALUES('2','The lazy fox jumps againbunch of other html<p><em>Is the lazy fox crazy?</em></p>')
INSERT INTO table VALUES('3','The lazy fox jumps againbunch of other html<p><em>Is the lazy fox crazy?</em></p>')
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.