どのようにして特定のパターン内で、好ましくはsedではなくawkなどで文字を置き換えることができますか? htmlのh3 IDのスペースをハイフン( - )に置き換えたいのですが、行全体をハイフンにしたくありません。私foo.htmlというにおける行内のSed置換パターン
例えば:私が欲しいもの
<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>
は、このようなH3Sです:
<h3 id="Another-id-that-should-be-spaceless">Spaces please!</h3>
私は
sed -e "/^<h3 id=\"/,/\">/s/ /-/g;" <foo.html >bar.html
を試みた。しかし、これは貪欲にハイフンを追加ハイフンを付けるべきでない行(2番目のp)と部分(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 - 5。 – kokoro