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 |
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. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
07-16-2010, 06:05 PM
|
#1
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Rep:
|
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.
|
|
|
|
07-16-2010, 07:05 PM
|
#2
|
|
Member
Registered: Nov 2004
Location: Arcadia, CA
Distribution: Gentoo, Sabayon, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 201
Rep:
|
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.
|
|
|
|
07-16-2010, 08:26 PM
|
#3
|
|
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 2,487
Original Poster
Rep:
|
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.
|
|
|
|
07-26-2010, 02:03 PM
|
#4
|
|
Member
Registered: Nov 2004
Location: Arcadia, CA
Distribution: Gentoo, Sabayon, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 201
Rep:
|
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.
|
|
|
|
| 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 10:49 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
|
|