LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-27-2008, 01:59 PM   #1
Harry Seldon
Member
 
Registered: Feb 2004
Distribution: SLES 9
Posts: 112

Rep: Reputation: 15
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?
 
Old 05-27-2008, 02:46 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try grep -x.
 
Old 05-27-2008, 02:51 PM   #3
Harry Seldon
Member
 
Registered: Feb 2004
Distribution: SLES 9
Posts: 112

Original Poster
Rep: Reputation: 15
That's exactly what I needed. Looks like I got my man lines crossed when I was looking at the flags. Thanks, colucix.
 
Old 05-27-2008, 02:59 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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)
 
Old 05-27-2008, 06:47 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
grep ^test.txt$ <yourfile>
 
Old 05-27-2008, 07:58 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"-w" ...
 
  


Reply



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
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
writing a c code for scanning files for certain phrases raklo Programming 3 06-21-2007 11:47 AM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM
A simple question. turning off phrases. anton_w Slackware 3 09-21-2005 06:27 AM

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

All times are GMT -5. The time now is 07:34 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
Open Source Consulting | Domain Registration