Grep / awk基于定义的模式的单行或多行(从模式开始并以定义的模式结束)

时间:2018-05-09 19:17:48

标签: linux awk grep

我需要帮助“点击”从“TEST FILE”开始到“;”的行。我对任何其他方法如awk

持开放态度

文件A包含以下内容:

TEST FILE ABC
this file contains ABC;

TEST FILE DEF
this file contains DEF
;

TEST FILE (DEF) this file contains (DEF);
TEST FILE DGHT this file contains DGHT;

TEST FILE 123
this file contains ABC,
this file contains DEF,
this file contains XYZ,
this file contains KLM
;

我想要以下5个文件

File1中

TEST FILE ABC
this file contains ABC;

文件2

TEST FILE DEF
this file contains DEF
;

文件3

TEST FILE (DEF) this file contains (DEF);

FILE4

TEST FILE DGHT this file contains DGHT;

File5

TEST FILE 123
this file contains ABC,
this file contains DEF,
this file contains XYZ,
this file contains KLM
;

1 个答案:

答案 0 :(得分:0)

关注awk可能对您有帮助。

awk '/TEST/{flag=1;++i} flag{print > "file"i} /;$/{flag=""}' Input_file

现在也添加非单线形式的解决方案。

awk '
/TEST/{
       flag=1;
       ++i}
flag{
       print > "file"i}
/;$/{
       flag=""}
'   Input_file