ファイル内に2つのパターンが出現するたびに、その間の行にテキストを追加します。

ファイル内に2つのパターンが出現するたびに、その間の行にテキストを追加します。

これはかなり難しい問題になると思います。

オンライン ドキュメントとして使用するために HTML ファイルに抽出されるインライン ドキュメントをいじる必要がありますが、ファイルのこれらの部分は、
インライン形式では HTML タグがなく、抽出された HTML ファイルにのみ HTML タグがない必要があります。ただし、これらのドキュメント部分も .wiki ファイルに抽出されるため、一部のタグは既にこのように存在しています。

this is some text describing what is done
<code>
here are 
some line that will be shown as code in wiki 
but not on html cause they are shown on one line
in html output
</code>

some more describing text
<code>
another piece of code 
that shows up as multiple lines in the wiki
but not in htmls
</code>

ドキュメントのこれらの部分を抽出した後 (これは sed で簡単に実行できます)、抽出したファイルを次のように sed します。

this is some text describing what is done
<code><br/>
here are <br/>
some line that will be shown as code in wiki <br/>
but not on html cause they are shown on one line<br/>
in html output<br/>
</code><br/>

some more describing text
<code><br/>
another piece of code <br/>
that shows up as multiple lines in the wiki<br/>
but not in htmls<br/>
</code><br/>

これまでに取得したのは次の sed 行です。

sed -i '/\<code>/,/\<\/code>/{s/$/\<br\/>/}' file

ただし、次のようにコード領域間のテキストにも HTML タグが追加されます。

this is some text describing what is done
<code><br/>
here are <br/>
some line that will be shown as code in wiki <br/>
but not on html cause they are shown on one line<br/>
in html output<br/>
</code><br/>
<br/>
some more describing text<br/>
<code><br/>
another piece of code <br/>
that shows up as multiple lines in the wiki<br/>
but not in htmls<br/>
</code><br/>

sed は最初のand the lastタグ間のすべての行に追加するので、これは基本的に正しいのですが、私が意図したものではありません。

ここで何が欠けているかについて、誰かヒントをくれませんか?

答え1

\<バックスラッシュが間違っています。この式はないリテラルの左の改行に一致します。エスケープされていない文字<はそれ自体に一致しますが、バックスラッシュを使用すると、左の単語境界のゼロ幅アサーションに変換されます。これはスラッシュの隣では決して発生しないため、式は\</code>何も一致しません。

他の余分な過剰修正も修正するための若干のリファクタリングにより、修正されたsedスクリプトは

sed -i '/<code>/,\%</code>%s:$:<br/>:' file

バックスラッシュの必要性をさらになくすために、スラッシュを別のものに変更することにしました。

デモ:http://ideone.com/feVWgO

答え2

OK、解決策が見つかりました。ただしsed、 ではなく を使用しましたawk

awk '
  BEGIN{c=0} // initialize variable with zero
  /\<code>/{c=1} // if start pattern found set variable to 1
  {if (c==1) print $0,"<br/>"} // if variable is one, append tag
  {if (c==0) print $0} // if variable is zero just print line
  /\<\/code>/{c=0} //if endpattern is found set variable to zero
  '

これは実は非常にシンプルですがエレガントです。

関連情報