Linux 如何通过匹配shell脚本中的模式来组合两行

Linux 如何通过匹配shell脚本中的模式来组合两行,linux,scripting,Linux,Scripting,我试图把这两条线结合起来。 我的数据示例如下: Hello Reach World Test Reach me Test out . 我想将其合并为: Output Hello Reach World Test Reach me Test out .i.e Only if last word matches Test and Begin matches Reach . 我在试着用 awk'/Test$/{printf(“%s\t”,$0);next}1' 任何人都可以告诉我如何匹配和组合。

我试图把这两条线结合起来。 我的数据示例如下:

Hello Reach World Test
Reach me Test out .
我想将其合并为:

Output
Hello Reach World Test Reach me Test out  .i.e Only if last word matches Test and Begin matches Reach .
我在试着用 awk'/Test$/{printf(“%s\t”,$0);next}1'


任何人都可以告诉我如何匹配和组合。此awk脚本是否符合您的要求:

BEGIN { flag = "0"; line = "" }
{
    if ( flag == "1" ) {
        if ( $0 ~ "^Reach" )
            print line " " $0
        else {
            print line
            print $0
        }
        line = ""
        flag = "0"
    } else {
        if ( $0 ~ "Test$" ) {
            line = $0
            flag = "1"
        } else
            print $0
    }
}

为了澄清,您只是试图将所有出现的“Test\nReach”替换为“Test Reach”,对吗?Try:
perl-0777-pe's/Test\n访问/Test到达/g;'输入
这可能是一个dup:不,我的输入将类似于下一行reach中的测试,我想使用匹配模式Test和reach组合成一行。