Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading