LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2018, 11:48 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Rep: Reputation: Disabled
force to grep adjacent words


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

Last edited by udiubu; 07-04-2018 at 12:01 PM.
 
Old 07-04-2018, 12:12 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
It looks like the -B is not needed.

Then change the span (.*) so that it does not include spaces.

Code:
grep --no-group-separator -n " det|t[^[:space:]]* n|${WORD}" input.txt
If you need fancier and have GNU's grep then you can use perl regular expressions instead and take advantage of lookahead or lookbehind matches.
 
1 members found this post helpful.
Old 07-04-2018, 12:30 PM   #3
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
This works beautifully!
Thanks Turbocapitalist!
 
  


Reply

Tags
grep, strings



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep for words with special characters dwezel Linux - General 5 07-07-2012 03:23 PM
Can I use grep to find two words near each other? walterbyrd Linux - Software 4 12-09-2011 11:01 PM
how to grep 2 words at the same time ufmale Linux - Newbie 2 09-09-2010 04:25 PM
Grep words and paste is on the same line say_hi_ravi Programming 7 10-31-2008 06:56 AM
Can grep filter out words? extrasolar Linux - General 1 07-20-2006 03:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration