我有一个()脚本,它包含纯文本,加上分隔的bash代码块。我想将所有bash代码块提取到一个单独的文档中,标题作为代码上方的块作为注释。但是,我只希望在找不到选项include和echo时打印代码块。示例脚本:
Here's some writing. Next code chunk should be included.
```{bash, chunk-1, option=TRUE}Foo=“酒吧”
Now one not to be included because it contains `include`.
```{bash, chunk-2, option=TRUE, include=FALSE}foo2="bar2“
Another code chunk not to be included because it contains `echo`.
```{bash, chunk-3, option=FALSE, echo=FALSE}foo3="bar3“
Finally one that should be included because `include` and `echo` are not found.
```{bash, chunk-4, option=FALSE, another_option=TRUE}foo4="bar4“
期望的输出是
# chunk-1
foo="bar"
# chunk-4
foo4="bar4"块标题总是在前两个逗号之间,后面的其他选项是逗号分隔的。
我一直在和awk合作。下面将获得注释写入,但不会搜索include或echo。
awk '$0 ~ /^```$/ {p=0}
$1 ~ /^```{bash/ {p=1; print "\n# " substr($2, 1, length($2)-1); next}
p'发布于 2020-08-25 14:43:40
您可以使用此awk
awk -F ', *' '/^```$/{p = 0} p;
/```{bash/ && !/ (include|echo)=/{print "#", $2; p=1}' file# title-of-chunk
# here's some bash code
blah_blah="blah"https://stackoverflow.com/questions/63580761
复制相似问题