LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-25-2008, 12:40 PM   #1
eh270
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Rep: Reputation: 0
Help with grep script


I'm familiar with a handful of linux/unix command line tools, but don't know enough about scripting to do this. I'm trying to put together a shell script that will do the following:
  • get a list of all files in a particular directory
  • for each file, run grep -c for two defined patterns (e.g., grep -c foo, grep -c bar)
  • append the results of each grep to a file with this format for each line (without brackets)
{filename} {grep pattern} {count results}

I suspect i need a 'for each' loop ... this seems like it shoudl be pretty easy to do.

Thanks in advance for your help!
 
Old 01-25-2008, 01:06 PM   #2
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
This shouldn't be difficult via shell/bash scripting, but since my background is in perl, I came up with a little ditty to help point you in the right direction (this is untested but the theory should be sound):

Code:
#!/usr/bin/perl
#
@filelist = </path/to/directory/*>;
foreach $file (@filelist) {
   $foo = 0;
   $bar = 0;

   $foo = `grep -c foo $file`;
   $bar = `grep -c bar $file`;

   print "$file -- foo -- $foo\n" if ($foo);
   print "$file -- bar -- $bar\n" if ($bar);
}
The above could be done with perl's built-in grep function, but as I said this was a quick-and-dirty script. The above would output the # of occurrences of each word to STDOUT, where you could redirect it anywhere you'd like.
 
Old 01-25-2008, 01:20 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This sounds like homework......

What text are you using? eg Bash Guide for Beginners (free at http://tldp.org)

Do you want to grep for patterns in the filename or in the contents?
contents:
for i in *; do grep pattern $i; done

filename:
ls|grep pattern
or
ls -R|grep pattern
 
Old 01-25-2008, 03:29 PM   #4
eh270
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Poetics View Post
The above could be done with perl's built-in grep function, but as I said this was a quick-and-dirty script. The above would output the # of occurrences of each word to STDOUT, where you could redirect it anywhere you'd like.
this is great, thanks a ton! how would i put it to a file, though, instead of stdout?
 
Old 01-26-2008, 07:05 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by eh270 View Post
this is great, thanks a ton! how would i put it to a file, though, instead of stdout?
Redirection.....

Any command that writes to the screen can be redirected to a file using "> filename"
eg:
ls > listing creates a file with the output of the ls command
 
  


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
script writing help (grep -r) krazyglue Programming 9 11-21-2007 10:16 AM
grep script kaiserbeto Linux - Newbie 2 11-15-2006 01:50 PM
grep script/syntax carriehoff Linux - Newbie 6 08-31-2006 01:13 PM
Shell script with grep MicahCarrick Programming 4 08-15-2006 01:08 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

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

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