LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to grep for an exact phrase (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-grep-for-an-exact-phrase-4175500493/)

ananny 04-03-2014 03:37 PM

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"

schneidz 04-03-2014 03:44 PM

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.
...


ananny 04-03-2014 03:52 PM

Quote:

Originally Posted by schneidz (Post 5146141)
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

colucix 04-03-2014 04:31 PM

By means of extended regular expressions (option -E)
Code:

grep -Ew 'stat|station'

ananny 04-03-2014 05:19 PM

Quote:

Originally Posted by colucix (Post 5146167)
By means of extended regular expressions (option -E)
Code:

grep -Ew 'stat|station'

Thanks. I have a lot to learn. I'll get started!


All times are GMT -5. The time now is 05:47 PM.