0

I was trying to combine the two lines. example of my data is ::

Hello Reach World Test
Reach me Test out .

I would like to combine this as ::

Output
Hello Reach World Test Reach me Test out  .i.e Only if last word matches Test and Begin matches Reach .

I was trying with awk '/Test$/ { printf("%s\t", $0); next } 1' .

Could anyone please let me know how to match it and combine.

Shilpa
  • 103
  • 1
  • 9
  • 1
    To clarify, you are just trying to replace all occurrences of "Test\nReach" with "Test Reach", right? Try: `perl -0777 -pe 's/Test\nReach/Test Reach/g;' input` – William Pursell Feb 03 '17 at 14:44
  • this may be a dup: http://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed – General Foch Feb 03 '17 at 15:32
  • No , My input will be like Test in next line reach ,I want to combine into single line using the matching pattern Test and Reach . – Shilpa Feb 03 '17 at 17:08

1 Answers1

0

Does this awk script do what you want:

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
    }
}
Leslie
  • 618
  • 4
  • 14