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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-27-2008, 01:59 PM
|
#1
|
|
Member
Registered: Feb 2004
Distribution: SLES 9
Posts: 112
Rep:
|
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?
|
|
|
|
05-27-2008, 02:46 PM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
You can try grep -x.
|
|
|
|
05-27-2008, 02:51 PM
|
#3
|
|
Member
Registered: Feb 2004
Distribution: SLES 9
Posts: 112
Original Poster
Rep:
|
That's exactly what I needed. Looks like I got my man lines crossed when I was looking at the flags.  Thanks, colucix.
|
|
|
|
05-27-2008, 02:59 PM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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)
|
|
|
|
05-27-2008, 06:47 PM
|
#5
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
grep ^test.txt$ <yourfile>
|
|
|
|
05-27-2008, 07:58 PM
|
#6
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,288
|
"-w" ... 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:04 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|