在组织标签之间打印文本

在组织标签之间打印文本

我想编写一个 bash 函数,打印文件中匹配的行## mode: org和之间包含的文本部分,各部分之间有一个空行。## # 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:]]*## //'

这将删除开始和结束模式之外的所有内容。然后它将删除开始模式并用空行替换结束模式(成为块分隔符)。

相关内容