組織ラベル間のテキスト印刷

組織ラベル間のテキスト印刷

## mode: orgファイル内の一致する行とその間に含まれるテキスト部分を各部分の間に空白行で印刷するbash関数を作成したいと思います。## # End of org前には##空白がいくつかあります。

以下は、情報が抽出されたファイルの例です。

file: test.sh

## mode: org
## * Using case statement
## # End of org
case $arg in
 ("V")
   echo "Author"
   ;;
 (*)
   ## mode: org
   ## ** Silent Error Reporting Mode (SERM) in getopts
   ## *** Detects warnings without printing built-in messages.
   ## *** Enabled by colon {:} as first character in shortopts.
   ## # End of org
   break
   ;;
esac

希望の出力は

* Using case statement

** Silent Error Reporting Mode (SERM) in getopts
*** Detects warnings without printing built-in messages.
*** Enabled by colon {:} as first character in shortopts.

私がやった

capture-org ()
{
  sed -n '/^ *## mode: org$/,/^ *## # End of org$/s/ *//p' "$1" |
   sed 's/^## mode: org$/\n## mode: org/' |
   sed '/^## mode: org$/d' | sed '/^## # End of org$/d' | cut -c 3-
}

もっと簡潔な方法で行うことができますか?

ベストアンサー1

sed '/^[[:space:]]*## mode: org$/,/^[[:space:]]*## # End of org$/!d; /^[[:space:]]*## mode: org$/d; s/^[[:space:]]*## # End of org$//g; s/^[[:space:]]*## //'

開始パターンと終了パターンを除くすべての項目が削除されます。次に、開始パターンを削除し、終了パターンを空白行(ブロック区切り文字)に置き換えます。

おすすめ記事