Thursday, July 28, 2022

[SOLVED] Find matching words

Issue

I have a corpus file and the rules file. I am trying to find matching words where the word from rule appear in corpus.

# cat corpus.txt
this is a paragraph number one
second line
third line

# cat rule.txt
a
b
c

This returns 2 lines

# grep -F0 -f rule.txt corpus.txt
this is a paragraph number one
second line

But I am expecting 4 words like this...

a
paragraph
number
second

Trying to achive these results using grep or awk.


Solution

Assuming words are seperated by white spaces

awk '{print "\\S*" $1 "\\S*"}' rule.txt | grep -m 4 -o -f - corpus.txt



Answered By - IamNOTaROBOT
Answer Checked By - Gilberto Lyons (WPSolving Admin)