从包含起始行和结束行的匹配模式的文件中读取行并将其附加到 bash 中的另一个文本文件

Read lines from a file enclosed matching pattern of starting line and ending line and append it to another text file in bash

我必须从命令行中接收到的输入文件中读取特定行并更改所有行并将其附加到另一个文本文件中。

输入文件有这些行(这组行只出现一次):

00 TOOL     | Value of SIMIN:
00 TOOL     |   /absolute/path/to/some/required/file_1
00 TOOL     |   /absolute/path/to/some/required/file_2
00 TOOL     |   /absolute/path/to/some/required/file_3
00 TOOL     |   
00 TOOL     | Value of SIMOUT:

我们需要阅读的线条介于两个特定的图案之间。

起始模式:

00 TOOL     | Value of SIMIN: 

结束模式:

00 TOOL     |   
00 TOOL     | Value of SIMOUT:

稍后我需要将其转换为以下行并将其附加到另一个文件:

export SIMIN=/absolute/path/to/some/required/file_1:$SIMIN
export SIMIN=/absolute/path/to/some/required/file_2:$SIMIN
export SIMIN=/absolute/path/to/some/required/file_3:$SIMIN

从此post了解了如何逐行读取文件。

但在我的要求中,我可能需要一次读取缓冲区中的文件,搜索上述线条模式并选择中间的线条并进行修改。

任何 help/suggestion 将不胜感激!

能否请您尝试以下。

awk '
/SIMOUT/{
  flag=""
}
/SIMIN/{
  flag=1
}
flag && match([=10=],/\/[^ ]*/){
  print "export SIMIN=" substr([=10=],RSTART,RLENGTH)":$SIMIN"
}
'  Input_file

如果您想将其输出到输出文件中,请将 > output_file 添加到上面的代码中。

编辑: 在这里也添加了 ED 先生的解决方案。

awk '/SIMOUT/{f=0} f&&/\//{printf "export SIMIN=%s\n", $NF} /SIMIN/{f=1}' Input_file