sed:替换每一行中第一次出现的字符串

Mar*_*oen 5 sed find-and-replace

想象一下我有一个这样的文件:

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>')
Run Code Online (Sandbox Code Playgroud)

我想删除只有第一次出现的<p><em></em></p>,所以我最终的东西是这样的:

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>')
Run Code Online (Sandbox Code Playgroud)

...我怎么能用sed(或perl)做到这一点?该声明...:

sed "1,/INSERT INTO/s/<p><em>//g"
Run Code Online (Sandbox Code Playgroud)

... 只替换文件中的第一次出现,而不是每一行。

非常感谢帮助。

cho*_*oba 4

如果要处理带有 的所有行INSERT INTO,请不要提供地址范围。如果您只想替换第一次出现的字符串,请不要提供/g

sed -e '/INSERT INTO/s/<p><em>//' -e '/INSERT INTO/s/<\/em><\/p>//' 
Run Code Online (Sandbox Code Playgroud)