我该如何才能在特定模式中替换字符,最好在sed但awk或其他方式,如果有更简单的选项?我想用连字符( - )替换我的html h3 id中的空格,但我不想让它连字符连接整行。sed替换一行内的模式
例如,在我的foo.html:
<p>This is a paragraph which likes its spaces.</p>
<h3 id="No spaces in this id please">Keep spaces in this title</h3>
<p>Here's another paragraph with spaces.</p>
<h3 id="Another id that should be spaceless">Spaces please!</h3>
<p>Yes I would like extra noodles in my soup.</p>
我想什么是3H公司这样的:
<h3 id="Another-id-that-should-be-spaceless">Spaces please!</h3>
我已经试过
sed -e "/^<h3 id=\"/,/\">/s/ /-/g;" <foo.html >bar.html
但这种贪婪地增加了连字符。到不应该有连字符的行(第2个)和部分(h3内容)! Bar.html:
<p>This is a paragraph which likes its spaces.</p>
<h3-id="No-spaces-in-this-id-please">Keep-spaces-in-this-title</h3>
<p>Here's-another-paragraph-with-spaces.</p>
<h3-id="Another-id-that-should-be-spaceless">Spaces-please!</h3>
<p>Yes I would like extra noodles in my soup.</p>
注意我正在使用GNU sed。谢谢!
你只需要这一点,4号线? –
不,整个文件。 SLePort的答案就是这样。谢谢k-five。 – kokoro