我該如何才能在特定模式中替換字符,最好在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