Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash查找字符串之间的文本并替换找到的字符串中的文本_Bash_Awk_Sed_R Markdown - Fatal编程技术网

使用bash查找字符串之间的文本并替换找到的字符串中的文本

使用bash查找字符串之间的文本并替换找到的字符串中的文本,bash,awk,sed,r-markdown,Bash,Awk,Sed,R Markdown,我有一个(nR Markdown)脚本,它包含纯文本和分隔的bash代码块,如下所示: ```{bash, title-of-chunk, other=TRUE, options=FALSE} # here's some bash code blah_blah="blah" ``` 我想将所有bash代码块提取到一个单独的文档中。问题是,我希望不同的代码块都有一个注释标题。所以结果看起来像 # title-of-chunk # here's some bash code b

我有一个(nR Markdown)脚本,它包含纯文本和分隔的bash代码块,如下所示:

```{bash, title-of-chunk, other=TRUE, options=FALSE}
# here's some bash code
blah_blah="blah"
```
我想将所有bash代码块提取到一个单独的文档中。问题是,我希望不同的代码块都有一个注释
标题
。所以结果看起来像

# title-of-chunk
# here's some bash code
blah_blah="blah"
区块标题将始终位于前两个
之间。 我一直在使用
sed
,但还没有找到正确的组合

awk '$0 ~ /^```$/ {p=0} 
     $1 ~ /^```{bash/ {p=1; print "\n# " substr($2,1,length($2)-1); next}
     p' file >  output
我已扩展了您的示例输入:

> cat file
blah blah

```{bash, title-of-chunk, other=TRUE, options=FALSE}
# here's some bash code
blah_blah="blah"
```
blah
blah

```{python, test}
# here's some python code
blah_blah="blah"
```

test

```{bash, exit, other=TRUE, options=FALSE}
# here's some bash code
exit 0
```
输出:

> cat output

# title-of-chunk
# here's some bash code
blah_blah="blah"

# exit
# here's some bash code
exit 0

仅根据显示的样品,请您尝试以下一次。现场书写和测试

说明:添加上述内容的详细说明

awk '                                       ##Starting awk program from here.
/^```/{                                     ##Checking condition if line has ``` then do following.
  found=""                                  ##Nullify found here.
}
match($0,/^```{.*title-of-chunk/){          ##Using match function to match ```{bash till title-of-chunk then do following.
   val=substr($0,RSTART,RLENGTH)            ##Sub-string of matched regex output is being saved in variable here.
   sub(/.*title/,"title",val)               ##Substituting everything till title with title in in val.
   print "#"val                             ##Printing # and val here.
   val=""                                   ##Nullifying val here.
   found=1                                  ##Setting found to 1 here.
   next                                     ##next will skip all further statements from here.
}
found                                       ##Checking condition if found is SET then print the current line.
'  Input_file                               ##Mentioning Input_file name here.
awk '                                       ##Starting awk program from here.
/^```/{                                     ##Checking condition if line has ``` then do following.
  found=""                                  ##Nullify found here.
}
match($0,/^```{.*title-of-chunk/){          ##Using match function to match ```{bash till title-of-chunk then do following.
   val=substr($0,RSTART,RLENGTH)            ##Sub-string of matched regex output is being saved in variable here.
   sub(/.*title/,"title",val)               ##Substituting everything till title with title in in val.
   print "#"val                             ##Printing # and val here.
   val=""                                   ##Nullifying val here.
   found=1                                  ##Setting found to 1 here.
   next                                     ##next will skip all further statements from here.
}
found                                       ##Checking condition if found is SET then print the current line.
'  Input_file                               ##Mentioning Input_file name here.