LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-24-2007, 11:21 AM   #1
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
Recursive Find


I want to find and count a name that appears in a series of documents in a directory. I figure that I want a recursive find command and then grep that to find the name like this:

Code:
find . file* | xargs grep "John Doe"
This is where I am getting hung up on the syntax. How do I count all instances of "John "Doe" and output that number?
 
Old 05-24-2007, 11:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Greps -c option will count the instances for you (man grep):

find . file* | xargs grep -c "John Doe"

Hope this helps.
 
Old 05-24-2007, 11:28 AM   #3
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna
Hi,

Greps -c option will count the instances for you (man grep):

find . file* | xargs grep -c "John Doe"

Hope this helps.

I want a total count. This give me how many times the name appears per file.
 
Old 05-24-2007, 11:50 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Ok, I misunderstood, but this should do what you want:

find . -name "file*" -exec grep -c "John Doe" {} \; | awk '{ cntr = cntr + $0 } END { print cntr } '

I placed the grep part inside the find using -exec (not really necessary, but it looks better). The output (individual numbers representing the amount of hits) are given to awk. Awk adds the numbers and prints the end result.
 
Old 05-24-2007, 12:15 PM   #5
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
That works


Thanks
 
Old 05-24-2007, 01:14 PM   #6
Linville79
Member
 
Registered: Nov 2006
Location: Indiana, U.S.A.
Distribution: Red Hat Enterprise Linux, Fedora, CentOS
Posts: 134

Rep: Reputation: 16
I would think that something like this would be much more simple to use.

find . file* | xargs grep "John Doe" | wc

Just pipe your find and grep into wc (word count).

Anyway, whatever works, works. :-)
 
Old 05-24-2007, 01:28 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

@Linville79: Shorter and faster. You win
 
  


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
writing a recursive 'find' command nadroj Programming 5 03-06-2007 02:06 AM
recursive find dhaus111 Linux - Newbie 10 01-11-2007 01:59 AM
Recursive, comm, find, PDFs zamdrist Linux - General 3 11-13-2006 11:56 PM
rm -r what is recursive wogga Linux - Software 3 05-28-2004 03:29 PM
call recursive find except on ./foo/ Hano Linux - General 1 07-30-2002 02:08 PM


All times are GMT -5. The time now is 03:02 PM.

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