LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-16-2010, 06:05 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,125

Rep: Reputation: 46
grep and path name matching.


Linux kernel 2.6, Slackware 12.0.
Hi:

Question: I do grep -r string_1 .*txt. Is the path first expanded by the shell? If not, then say, ./elem1/elem2/file1.txt would search for string_1. However, it will not in my system. Note: '.' (period) in .*txt is any char. '.' in ./elem1/elem2/file1.txt is current dir. Now, .*txt should be, as a regexp, zero or more occurrences of any char followed by txt, that is, any string with txt as a suffix. Thanks.

Last edited by stf92; 07-16-2010 at 08:27 PM.
 
Old 07-16-2010, 07:05 PM   #2
ShadowCat8
Member
 
Registered: Nov 2004
Location: Arcadia, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 209

Rep: Reputation: 43
Well,

If I were to try to find all files referencing string_1 in files that were ending in ".txt", I think I would try something like this:
Code:
~ $ grep -r string_1 --include=*.txt ./*
or, to make more sense of it and cut down possible false results:
Code:
find . -name "*.txt" -print | grep string_1
HTH. Let us know.
 
Old 07-16-2010, 08:26 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,125

Original Poster
Rep: Reputation: 46
Thanks. The first command seems to work fine, although it would be difficult to test for "false results". As for the second one, I think you have missed something, for grep would then be looking into the path names and not into the contents of the files themselves. Regards.
 
Old 07-26-2010, 02:03 PM   #4
ShadowCat8
Member
 
Registered: Nov 2004
Location: Arcadia, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 209

Rep: Reputation: 43
Greetings,

@stf92: You are correct. My fingers got ahead of my brain while I was typing and missed the (ahem...) cat. Hehe

So, really, the right way to do it for the second one would be something like:
Code:
for x in `find . -name "*.txt" -print`
     do
     cat ${x} | grep string_1
     if [ "$?" -eq "0" ]
          echo -e "File ${x} contains string_1.\n"
          fi
     done
HTH.
 
  


Reply

Tags
grep


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 05:36 PM
[SOLVED]matching a pattern with grep kdelover Programming 2 09-18-2009 06:07 AM
grep command for invert matching neeraj@linuxques Linux - Newbie 2 04-01-2009 08:25 AM
Matching a domain with grep abefroman Programming 2 11-06-2008 03:07 AM
how to use grep to print just the matching expression and nothing else new_2_unix Linux - Newbie 5 01-07-2008 05:02 PM


All times are GMT -5. The time now is 09:14 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