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
using grep and print filename ahjiefreak Shell Programming and Scripting 5 01-10-2008 11:47 AM
grep string and output filename happyv Shell Programming and Scripting 3 11-20-2007 12:16 AM
Get Filename and Line Number using grep ceemh3 UNIX for Dummies Questions & Answers 4 09-17-2007 10:35 AM
Grep all files matching partial filename mharley Shell Programming and Scripting 3 06-08-2005 02:17 PM
Losing filename in grep output netguy Shell Programming and Scripting 6 04-27-2004 11:39 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2007
sjohns6 sjohns6 is offline
Registered User
  
 

Join Date: May 2007
Posts: 3
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 (permalink)  
Old 05-23-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 (permalink)  
Old 05-23-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 05-23-2007
sjohns6 sjohns6 is offline
Registered User
  
 

Join Date: May 2007
Posts: 3
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 (permalink)  
Old 05-23-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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 (permalink)  
Old 05-24-2007
sjohns6 sjohns6 is offline
Registered User
  
 

Join Date: May 2007
Posts: 3
Perfect!!
Thank you!
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 05:15 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