LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-12-2011, 05:46 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
Further processing of find -exec


If I want to do a grep on the files on the whole server I can do the following:
Code:
find / -type f -exec grep '<what I want to grep>' '{}' \;
What if I want to pipe the "grepped" output for further processing? I can't just simply do this. It just doesn't work.
Code:
find / -type f -exec grep '<what I want to grep>' '{}' \; | grep '2nd level of grep'
How about I want to find the number of occurrences per file returned? For example, if the output of the first command above returns...
Code:
file 1: ad;flkjasd;lfk <what I want to grep> asdfl;kj
file 1: ad;flkjasd;lfk <what I want to grep> asdfl;kj
file 2: ad;flkjasd;lfk <what I want to grep> asdfl;kj
file 3: ad;flkjasd;lfk <what I want to grep> asdfl;kj
file 4: ad;flkjasd;lfk <what I want to grep> asdfl;kj
How do I structure the command so I can see something like?
Code:
file 1: 2
file 2: 1
file 3: 1
file 4: 1
 
Old 02-12-2011, 09:11 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

for the problem that you described try this first:
Code:
find . -type f -exec grep -cH 'pattern' '{}' \;
Then check this thread for other possibilities to further process the results:
http://www.linuxquestions.org/questi...o-slow-816525/

Finally, read the man-page for more info on how you can modify the output of grep. Chances are that you do not need the technique described in the linked post.

Hope this helps.
 
Old 02-12-2011, 09:27 AM   #3
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Rep: Reputation: 28
grep has a recursive option. So probably you can get rid of the find command if you wish. Something like this should give you the count of occurrences of your search pattern on all files in current directory and all its subdirectories:
Code:
grep -rc '<what I want to grep>' .
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find and -exec {} gleipnir Linux - Newbie 6 02-23-2010 05:22 PM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 07:13 PM
find -exec help dc144 Linux - Newbie 3 10-12-2006 02:14 AM
find -exec cricbk Linux - Newbie 4 01-05-2004 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:18 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
Open Source Consulting | Domain Registration