The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep and loop files kashik Shell Programming and Scripting 4 06-16-2008 09:41 AM
Grep commands in loop karthikn7974 Shell Programming and Scripting 0 04-28-2008 10:16 AM
grep and awk showing filename in loop sjohns6 Shell Programming and Scripting 5 05-24-2007 09:59 AM
grep -v while loop bobo UNIX for Dummies Questions & Answers 8 01-26-2007 10:53 PM
grep in a loop gundu Shell Programming and Scripting 7 03-28-2005 05:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-31-2008
foleyml foleyml is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 3
Grep Different Files Using a Loop?

I have a script to GREP for a text expression within certain files, the files being named file.11012008 thru file.11302008. 30 files in all, one for each day of the month.

Instead of entering the following 3 lines of code 30 different times, I'm trying to find a way to loop the process:

#insert the heading of the search
echo 11/01/2008 >> LOG_FILE

#insert blank line below the heading
echo "" >> LOG_FILE

#grep for the text and its entire line, then insert in log file.
grep "search_text" search/path/file.11012008 >> LOG_FILE

Thanks a million!
  #2 (permalink)  
Old 12-31-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,763
Code:
grep "search_text"  search/path/file.11*2008 > outputfile
Does this do what you want?
  #3 (permalink)  
Old 12-31-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Try:

command:
Code:
for file in file11[0-3][0-9]2008 
do 
    printf "%s\n\n" $(sed s'#.*11\(..\)2008#11/\1/2008#' <<< $file) >> logfile
    grep "search_text" search/path/${file} >> LOG_FILE 
done
HTH Chris
  #4 (permalink)  
Old 12-31-2008
foleyml foleyml is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 3
Quote:
Originally Posted by Christoph Spohr View Post
Try:

command:
Code:
for file in file11[0-3][0-9]2008 
do 
    printf "%s\n\n" $(sed s'#.*11\(..\)2008#11/\1/2008#' <<< $file) >> logfile
    grep "search_text" search/path/${file} >> LOG_FILE 
done
HTH Chris
Chris, this outputs the search string of each file all into the logfile also, but it's not inserting the header or the blank line in the logfile (to separate the data according to the file they came from). Below is your code modified to the actual search criteria. Sorry about the change in path and file name values:

Code:
for file in event_demon.PRD.11[0-3][0-9]2008
do
    printf "%s\n\n" $(sed s'#.*11\(..\)2008#11/\1/2008#' << $file) >> logfile
        grep "sa_ZALE_REFMT_8" /autos/autotree/autouser/out/${file} >> LOG_FILE
        done
I now just need to figure how to work in the 2 "echo" code lines from my first post.
  #5 (permalink)  
Old 12-31-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Yes, there is a little typo.
The headers go to: logfile
The search string to LOG_FILE

printf "%s\n\n" ... means print the string followed by two
linefeeds, so it takes care of your two echo commands in
one command. The string to print is the via sed adjusted
filename.
  #6 (permalink)  
Old 01-02-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
hi, something likes below, you may amend it to address your question
Code:
for i in file.*
do
	echo $i >> out.log
	echo >> out.log
	grep LEO $i >> out.log
	echo "------$i-----" >> out.log
done
  #7 (permalink)  
Old 12-31-2008
foleyml foleyml is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 3
Quote:
Originally Posted by jim mcnamara View Post
Code:
grep "search_text"  search/path/file.11*2008 > outputfile
Does this do what you want?
Jim, the command does output all the search strings from the differents files to the logfile, but i'd still have to format the logfile so that each search string has a header. I need to make the other two lines of code do that for each string.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0