Grep Different Files Using a Loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Different Files Using a Loop?
# 1  
Old 12-31-2008
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  
Old 12-31-2008
Code:
grep "search_text"  search/path/file.11*2008 > outputfile

Does this do what you want?
# 3  
Old 12-31-2008
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  
Old 12-31-2008
Quote:
Originally Posted by jim mcnamara
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.
# 5  
Old 12-31-2008
Quote:
Originally Posted by Christoph Spohr
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.
# 6  
Old 12-31-2008
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.
# 7  
Old 01-02-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ps ax with grep in loop

Hello, I have built the following script to check if processes supplied by the argument are running or not. #!/bin/bash PROCLIST=$1 PROCESS="0" ERROR_PROCS="" IFS='+' read -ra ADDR <<< "$PROCLIST" for PROC in "${ADDR}"; do if ; then PROCESS=1 ... (9 Replies)
Discussion started by: nms
9 Replies

2. UNIX for Beginners Questions & Answers

Loop grep, outputs in files

Hi, I try to create a simply code but I have problems... Ubuntum, Bash version: 4.3.46 Bash INPUT file (csv): TS133_29BC,xx2274305 TS133_29BC,rps4576240 av137_37BC,wfs2274305 av137_37BC,ftrs4576240 T1S138_30BC,rfss2255526 T1S138_30BC,rgas2274305 OUTPUT files (csv): File TS133_29BC... (6 Replies)
Discussion started by: echo manolis
6 Replies

3. Homework & Coursework Questions

GREP loop

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to search through the users home directories for keywords, display them. The code listed below will show... (7 Replies)
Discussion started by: jcllns1
7 Replies

4. Red Hat

using grep in a while loop

Hello everybody, I have been searching it, but it seems I am unable to find the correct information, that s why I am asking you guys, hoping somebody get an idea. Here is my problem : I want a script to loop until a string is identified in a log file. Here is the script : #!/bin/sh... (5 Replies)
Discussion started by: guyiom
5 Replies

5. Shell Programming and Scripting

Help on grep in a do while loop

So this is what I'm trying to do: I have a file called registry.txt which has a list of registry entries I want to search for. I have another file called inctrl.txt on which I want to perform the search on. Here's the example contents of registry.txt SOFTWARE\Microsoft\Security... (3 Replies)
Discussion started by: r4v3n
3 Replies

6. Shell Programming and Scripting

Using grep within a while loop

Hi all, I have the below script to get input but i cannot get grep to work. input1.txt AAAAAAAAG input2.txt >gi|184009.1| LEAFY-like |AAAAAAAAGSGGGDHLPY However, when i use grep -f input1.txt input2.txt i cannot get any output matches (note that the match is underlined). Is it... (8 Replies)
Discussion started by: turkishvan
8 Replies

7. Shell Programming and Scripting

grep and loop files

Hi , 1. I want to grep two or three lines from a set of files and put the grepped lines into again a set of files.like file1-greppedfile1 file2-greppedfile2 then again do some format to the grepped files with sed or awk then create another set of files. How can I do this in loop? 2.I... (4 Replies)
Discussion started by: kashik
4 Replies

8. Shell Programming and Scripting

Grep commands in loop

Hi All, Reference to my previous post I need to compare all the lines in the file1 with file2 for this condition if file1 {$3,$5} ==file2 {$3,$5} then grep file2{$1}latest date. need output in file3 10/04/2008 09/04/2008 09/04/2008 08/04/2008 can anyone suggest me Thanks... (0 Replies)
Discussion started by: karthikn7974
0 Replies

9. UNIX for Dummies Questions & Answers

grep -v while loop

alist contain: a b c d e blist contain: a b c the code: #!/usr/bin/ksh cat blist | while read line do grep -V "$line" alist > data done (8 Replies)
Discussion started by: bobo
8 Replies

10. Shell Programming and Scripting

grep in a loop

Hi , I am trying a script which takes user input userid . I am stuck how to check whether that is a valid user id or not in the audit log files. My code is : cd $CCP_AUDIT cat * > /export/home/$USR/l***/files echo "UserId:\c" read UserId #Date Function echo "DATE : \c" read xxx I... (7 Replies)
Discussion started by: gundu
7 Replies
Login or Register to Ask a Question