LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 02: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, 03:46 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can try grep -x.
 
Old 05-27-2008, 03: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, 03:59 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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, 07:47 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

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

Rep: Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069
"-w" ...
 
  


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


All times are GMT -5. The time now is 09:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration