LinuxQuestions.org
Review your favorite Linux distribution.
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 11-30-2017, 10:47 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Rep: Reputation: Disabled
grep in loop with exact match


Dear experts,

A rather simple issue I cannot manage to solve.

My infile:
alpha
alphabet
alpha-beta
beta
bebeta
gamma
ga-gamma

I want to retrieve exact match only.

Output:
alpha
beta
gamma

I'm using the following loop, but unsuccessfully.

for word in alpha beta gamma;
do
grep -w ${word} infile
done

Ideally,I'm looking for something like /^${word}$/
but I don't know exactly how to define spaces, given a variable.

Any help would be highly appreciated.

Udiubu
 
Old 11-30-2017, 11:18 AM   #2
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
double quote and curly brackets removal do the job

do
grep "^$word$" infile
done
 
Old 11-30-2017, 11:22 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
nevermind
 
Old 11-30-2017, 09:55 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by udiubu View Post
double quote and curly brackets removal do the job

do
grep "^$word$" infile
done
That works in situations where the word you are searching for takes up the whole line and there are not even any leading or trailing spaces.

If by 'exact match' you mean the string is not a substring but is a standalone word, then you need a more general approach for finding the whole word then you can also search for the null-string word boundaries. Most systems will refer to those as \< and \> for the start of a word boundary and the end of a word boundary respectively. So the following would find the word if it is whole, regardless of where on the line it is or how much of the line it takes up:

Code:
grep "\<${word}\>" infile;
Some systems might require it to be written a little differently,

Code:
grep "[[:<:]]${word}[[:>:]]" infile;
but that won't work with GNU grep
 
Old 12-02-2017, 07:31 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
For plain string search use fgrep (or grep -F)!
The -x option is for full line match.
Code:
for word in alpha beta gamma gam.a
do 
  fgrep -x "$word" infile
done
The standard grep would match gamma for both gamma and gam.a because the dot is an "any character".

Last edited by MadeInGermany; 12-02-2017 at 07:33 AM.
 
  


Reply

Tags
grep forloop, match



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
Using grep to find EXACT MATCH andy2008 Programming 19 02-14-2019 08:36 AM
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 04:34 AM
Scripting question grep exact match jackiebaron Linux - General 4 08-21-2011 09:55 AM
[SOLVED] Match datetime by the minute (not an exact match by the second) [mysql] hattori.hanzo Programming 1 10-21-2010 05:43 PM
how to match the exact pattern using grep utility vinaytp Linux - Newbie 3 05-11-2009 12:36 AM

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

All times are GMT -5. The time now is 07:46 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