Dear experts,
A very simple query regarding how to force grep to extract necessarily adjacent words in a while loop.
I have an input.txt file:
blabla
qq
det|theb-la
n|book
blabla
axc
det|thbl-abl
n|ball
blabla
aqq
det|thublabla adj|blue
n|book
and a list of words to be searched for (WORD.txt):
book
ball
I need to extract all exact adjacent occurrences of "
det|"
immediately followed by
n|WORD. "
det|" and "
n|WORD" are separated by a single space. I also need to get the line before match and line number. Please note that more alphanumeric chars can follow "
det|", before the single space occurs.
Expected output:
1: blabla
2: qq
det|theb-la
n|book
3: blabla
4: axc
det|thbl-abl
n|ball
So far my code below, which however also gets line 5 and 6.
How to tell "grep only adjacent match"?
And how to tell "any character following det, before space"? I am not sure the star there is really working.
Code:
while read WORD;
do
grep --no-group-separator -n -B1 " det|t.* n|${WORD}" input.txt
done < WORD.txt
Any help would be highly appreciated.
Sincerely,
Udiubu