How to compare if 2 successive lines from a file have matching pattern using awk script?

Wish to compare for first 4 matching words of successive lines while reading a text file. If they match join the 2 lines. Same for the next line and so on.
eg:
Sample text file:

I will party today evening with Neeta.
I will party today evening with Geeta.
I will party today evening with Sita.
You can go on with your friends.
She will be back today.
She will be back Tuesday.
So what can be done.

Expected Result:

I will party today evening with Neeta.I will party today evening with Geeta.I 
will party today evening with Sita.
You can go on with your friends.
She will be back today.She will be back Tuesday.
So what can be done.

>Solution :

An awk solution:

Executable awk file matching_words.awk:

#! /usr/bin/awk -f

{
    first_words = $1 "_" $2 "_" $3 "_" $4
    if (first_words == prec_words) {
        ret = ret $0
    } else {
        if (ret != "") {
            print ret
        }
        ret = $0
    }
    prec_words = first_words
}
END {
    if (ret != "") {
        print ret
    }
}

Used like that with your example in sample.txt file:

./matching_words.awk sample.txt

Leave a Reply