LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep for specific phrases (https://www.linuxquestions.org/questions/linux-newbie-8/grep-for-specific-phrases-645142/)

Harry Seldon 05-27-2008 01:59 PM

grep for specific phrases
 
Is there a way to grep for an absolute string without returning partial word matches? Let's say I'm pulling a directory listing and looking for a particular file called test.txt. A ls shows:

Some.File
test.txt
test.txt.bak

If I try to grep test.txt, I get test.txt and test.txt.bak. I just want to return test.txt. I tried using the -w command but it didn't work, I'm guessing because there's no special characters after .txt, like a space or tab, just a continuation of alphanumerics. Any way to do this?

colucix 05-27-2008 02:46 PM

You can try grep -x.

Harry Seldon 05-27-2008 02:51 PM

That's exactly what I needed. Looks like I got my man lines crossed when I was looking at the flags. :D Thanks, colucix.

pixellany 05-27-2008 02:59 PM

The way I read the manual, -x forces a match of the whole line.

How about grep "test.txt " or grep "test.txt[^.]"

(These don't work if test.txt is at the end of the line)

chrism01 05-27-2008 06:47 PM

grep ^test.txt$ <yourfile>

syg00 05-27-2008 07:58 PM

"-w" ... :rolleyes:


All times are GMT -5. The time now is 07:14 AM.