Using grep but printing a line more than once | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Using grep but printing a line more than once

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-22-2013
evelibertine evelibertine is offline
Registered User
 
Join Date: May 2011
Last Activity: 22 August 2014, 5:12 AM EDT
Posts: 193
Thanks: 94
Thanked 0 Times in 0 Posts
Using grep but printing a line more than once

Hi,

I am using the grep function to grep some lines from a text file. However, a line is grepped only once even if it matches the strings in the first file more than once. How can i change the code so that the line gets printed more than once?

Input file 1:

Code:
1 o p
1 a b
2 3 c
5 6 7

Input file 2:

Code:
o a c

Desired Output:

Code:
1 o p 
1 a b
2 3 c

Thanks!
Sponsored Links
    #2  
Old 01-22-2013
guruprasadpr's Avatar
guruprasadpr guruprasadpr is offline Forum Advisor  
Shrink...ing
 
Join Date: Jun 2009
Last Activity: 23 October 2014, 2:53 AM EDT
Location: India
Posts: 675
Thanks: 30
Thanked 215 Times in 214 Posts
This way:


Code:
$ grep -E "$(sed 's/ /|/g;' file2)" file1
1 o p
1 a b
2 3 c

Guru.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Help with grep printing multiple lines. m_usmanayub Shell Programming and Scripting 6 04-23-2012 08:32 AM
grep on string and printing line after until another string has been found rxc23816 Shell Programming and Scripting 7 07-23-2011 05:49 PM
Help required on grep command(Skip the first few lines from printing in the output) ks_reddy Shell Programming and Scripting 7 09-25-2009 10:40 AM
Need help in sed command [ printing a pattern + its line no or line no alone ] frozensmilz Shell Programming and Scripting 4 06-28-2009 10:38 AM
printing the next line too?? AndyA Shell Programming and Scripting 6 01-31-2008 05:35 AM



All times are GMT -4. The time now is 02:57 PM.