grep and awk showing filename in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and awk showing filename in loop
# 1  
Old 05-23-2007
grep and awk showing filename in loop

I am attempting to grep a list of files for a string an and then only extract the 3rd and 4th field of from the line. That's easy. But I want to prefix the line with the filename that the information came from.

for filename in `ls -1 *.txt'
do
grep search_text $filename | awk '{print $3" "$4}'
done

This displays the 3rd and 4th field of all lines in the list of files as in:
Field3 Field4
Field3 Field4

but I would like to see
a.txt Field3 Field4
b.txt Field3 Field4

Can somone give me a clue on how to do this.

Thanks,
Scott
f
# 2  
Old 05-23-2007
You can do something like that :
Code:
for filename in `ls -1 *.txt'
do
   awk '/search_text/ {print FILENAME,$3,$4}' $filename 
done

or (/dev/null in case where no file *.txt found'
Code:
awk '/search_text/ {print FILENAME,$3,$4}' *.txt /dev/null

Jean-Pierre.
# 3  
Old 05-23-2007
Sjohns,
Using your solution:
Code:
for filename in `ls -1 *.txt'
do
  OutStr=`grep search_text $filename | awk '{print $3" "$4}'`
  echo $filename $OutStr
done

# 4  
Old 05-23-2007
Thanks...this got me on the right track but I left something out that causes this solution to have a problem. The files are am searching through are gzipped. When I try to gunzip the file and pipe the data to awk, awk now things the FILENAME is standard in. Any thoughts?

for filename in `ls -1 *.txt.gz`
do
{ gunzip < $filename 2>>/dev/null; echo $?>>rc.txt; } | awk '/search_text/ {print $3,$4}'
done
# 5  
Old 05-23-2007
Try...
Code:
for filename in `ls -1 *.txt.gz`
do
{ gunzip < $filename 2>>/dev/null; echo $?>>rc.txt; } | awk -v f=$filename '/search_text/ {print f,$3,$4}' 
done

# 6  
Old 05-24-2007
Perfect!!
Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Grep showing all files name of the directory

Hi All, Can any one help me here.I'm getting whole files name of the directory along with the grep result. below is the code var=`grep -i '127.3.3.1' _record` echo $var (9 Replies)
Discussion started by: netdbaind
9 Replies

3. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

4. Shell Programming and Scripting

[awk] grep a part of filename as entry file

hi all, i need to combine these files into one csv file. Bounce_Mail_Event_Daily_Report_01_Jul_2012.csv Bounce_Mail_Event_Daily_Report_02_Jul_2012.csv Bounce_Mail_Event_Daily_Report_03_Jul_2012.csv Bounce_Mail_Event_Daily_Report_04_Jul_2012.csv... (10 Replies)
Discussion started by: makan
10 Replies

5. Shell Programming and Scripting

Grep command showing wrong output in Linux

Hi All I am trying to run a script in linux wherein i have a command like this grep ^prmAttunityUser= djpHewr2XFMAttunitySetup_ae1_tmp djpHewr2XFMAttunitySetup_ae1_tmp is a temporary file in which the user value is stored but this command in the script returns me balnk value whereas it has a... (4 Replies)
Discussion started by: vee_789
4 Replies

6. UNIX for Dummies Questions & Answers

Help with grep - not showing filenames

Hello, I'm looking for a search string within about 50 files but when the string is found it doesn't tell me in which member it has been found. Any ideas how I can do this? Cheers Rob (4 Replies)
Discussion started by: Grueben
4 Replies

7. Shell Programming and Scripting

grep/awk on a single column in a for loop

Hi I'm trying to loop through a small list of id's and then pull out a few columns if the id matches that found in column 2 of the larger file. I managed to get one command to work awk -F " " '{if ($2 == '154080196') print $2,$3,$4}' tst.txt | less However, when I try it in a for loop I... (3 Replies)
Discussion started by: flotsam
3 Replies

8. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

9. Shell Programming and Scripting

How to pull info under headers in file(awk,grep,while loop)

below is an extract from my file and I am trying to use Awk and grep and a while loop to pull infomation from under neath "HBA WWN=".HBA WWN=" reoccurs all over the file but the 100000c.....number are unique and I want to be able to pull and reference specifi information under this header ever time... (2 Replies)
Discussion started by: kieranfoley
2 Replies

10. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies
Login or Register to Ask a Question