Register
It is currently Fri Aug 22, 2014 9:41 am

GREP for more than just 1 line?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Sep 22, 2010 9:38 pm   

Joined: Mon Sep 20, 2010 7:56 pm
Posts: 6
heylo!
i was wondering how to "grep -H" a how text that contain one or more occurrence of a pattern and return the whole text with the filename at the start as opposed to a bunch of lines each with the filename attached?

ex: grep -H <pattern> <files>
returns
<file1>: <line with pattern>
<file1>: <line with pattern>
<file1>: <line with pattern>
<file2>: <line with pattern>
...

want
<file1>: <whole content of file>

thx!


Top
 Profile  
 PostPosted: Thu Sep 23, 2010 5:14 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
If there is only 1 file that's going to match or you want the output of every file that matched as 1 block of text then this'll do;
Code:
cat $(grep -l <pattern> <files>)


The grep command outputs the file(s) and cat then displays the contents.


Top
 Profile  
 PostPosted: Thu Sep 23, 2010 5:18 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Sorry, checking the OP again I see that you actually want the filename at the start of the output too;

Code:
for ifile in $(grep -l <pattern> <files>)
do
  echo -n "$ifile: "
  cat $ifile
done


Between the 2 answers hopefully you can extract what you're after :)


Top
 Profile  
 PostPosted: Fri Sep 24, 2010 8:47 am   

Joined: Mon Sep 20, 2010 7:56 pm
Posts: 6
thx for the reply! works great thx


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP