LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-13-2008, 03:02 AM   #1
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
searching within files. cat grep find


I am trying to search for some content in my text files.
This is what I do.
Quote:
cat `find . -name "*.txt"` | grep ACC
This does work, but I have like 100's of these files and I want it do display the filename with the output while it greps.
How do I do that?

I cant seem to work this out.
 
Old 10-13-2008, 03:25 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
How about a loop
Code:
for file in `find . -name "*.txt"`
do
    result=`grep ACC $file` 
    if [[ $? -eq 0 ]]
    then
        echo $file $result
    fi
done
 
Old 10-13-2008, 03:37 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Or eventually
Code:
find . -name "*.txt" -exec grep -H ACC {} \;
The -H option of grep forces to print the file name. Note that using the cat and pipe approach, the filename is lost, since the content of the files is passed as standard input. Anyway, the advice by Chris is good in respect of the good ol' rule "Keep It Simple"!
 
1 members found this post helpful.
Old 10-13-2008, 04:32 AM   #4
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Original Poster
Rep: Reputation: 49
Yup, thanks both of you.
I understand the file part getting lost. But -H was new to me. You always learn something new
 
Old 10-13-2008, 04:59 AM   #5
cyprinidae
Member
 
Registered: Oct 2008
Distribution: Fedora, CentOS, Crunchbang
Posts: 46

Rep: Reputation: 16
You can also do that without -H

Code:
find . -type f -exec grep ACC /dev/null {} \;
 
  


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
cat|grep|awk pudhiyavan Programming 6 07-14-2008 02:56 AM
cat and grep dipuasks Linux - General 11 04-21-2008 04:39 AM
Searching in files: How do I grep better? Elomis Linux - General 5 12-19-2007 02:52 AM
bash searching directries and find the files bekirsagir Programming 1 08-19-2005 05:37 AM
logrotate log files - keeping in order when using grep or cat and bash dmellem Linux - Software 6 02-11-2005 02:05 PM


All times are GMT -5. The time now is 01:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration