LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-03-2014, 03:37 PM   #1
ananny
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Rep: Reputation: Disabled
How to grep for an exact phrase


I'm running a script (not mine) that outputs many files of the form:
FileName.1234567 (where the numerical part is some random number)

In most cases it's easy to grep out a certain filename, since they're not similar, but I'm running into the following problem with similar filenames

I'm trying to grep out certain filenames, eg. stat.123456, but unfortunately I get other output that has the word stat in it. How can I do an exact grep to discard matches like Newstat.123456, stationary.123456 etc

I've been using the following command

./SomeScript.sh | grep "stat"
 
Old 04-03-2014, 03:44 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,061

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Code:
man grep
...
       -w, --word-regexp
              Select  only those lines containing matches that form whole words.  The test is that the matching substring must
              either be at the beginning of the line, or preceded by a non-word constituent character.  Similarly, it must  be
              either  at the end of the line or followed by a non-word constituent character.  Word-constituent characters are
              letters, digits, and the underscore.
...
 
1 members found this post helpful.
Old 04-03-2014, 03:52 PM   #3
ananny
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
Code:
man grep
...
       -w, --word-regexp
              Select  only those lines containing matches that form whole words.  The test is that the matching substring must
              either be at the beginning of the line, or preceded by a non-word constituent character.  Similarly, it must  be
              either  at the end of the line or followed by a non-word constituent character.  Word-constituent characters are
              letters, digits, and the underscore.
...
That worked. Thanks. Is there a way to do a combined grep to return both sets of results simultaneously, for say "stat" and "station" filenames? like results of the type stat.123456 and station.654321
 
Old 04-03-2014, 04:31 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
By means of extended regular expressions (option -E)
Code:
grep -Ew 'stat|station'
 
1 members found this post helpful.
Old 04-03-2014, 05:19 PM   #5
ananny
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
By means of extended regular expressions (option -E)
Code:
grep -Ew 'stat|station'
Thanks. I have a lot to learn. I'll get started!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to grep for an exact word? Arpitha10 Linux - Newbie 8 12-16-2012 06:23 AM
Using grep to filter phrase before a space ShanxT Linux - General 5 03-13-2009 01:20 AM
find exact phrase subnet_rx Linux - General 1 03-29-2008 08:28 PM
grep a phrase degraffenried13 Linux - General 1 04-04-2004 11:10 AM
grep [exact phrase] chrisfirestar Linux - General 2 02-09-2004 04:30 AM


All times are GMT -5. The time now is 10:52 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration