LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2009, 05:19 PM   #1
mjtruco
LQ Newbie
 
Registered: May 2009
Posts: 3

Rep: Reputation: 0
grep for multiple targets


I need to select multiple targets from a file.
I've used grep successfully before when using few targets. For example:

egrep 'target1|target2|target3|target4' test.txt > test.out

test.txt is a file that contains:
target1 A A A A B B B
target2 B B B B B B B
target3 D F F F F F F
target4 F F F F F F F
target5 G G G G G G G
etc..

How can I do the same thing but instead of using egrep 'target1|target2|target3|target4|etc' use a file that contains a list with all the 1000 targets I'm interested on. Something like:
target1
target2
target3
target4
.
.
.
target1000

Thanks,

mjtruco
 
Old 05-20-2009, 05:29 PM   #2
pattwo
LQ Newbie
 
Registered: Jun 2008
Location: Ontario, Canada
Distribution: Slackware
Posts: 7

Rep: Reputation: 5
man grep

Look in the "Matching Control" section for the '-f' switch

Does exactly what you're looking for.
 
Old 05-20-2009, 05:30 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
grep -f pattern_file file
Look at man grep for details.
 
Old 05-20-2009, 07:05 PM   #4
mjtruco
LQ Newbie
 
Registered: May 2009
Posts: 3

Original Poster
Rep: Reputation: 0
grep -f didn't work for file with targets

I've tried
grep -f file_targets
and it didn't work

I get the following error:

illegal option --f
Usage: grep -hblcnsviw pattern file ...

Do you know what is happening?

Thanks,

mjtruco
 
Old 05-20-2009, 07:11 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You're probably on an ancient version of Linux, or on a commercial
Unix like solaris.



Cheers,
Tink

Last edited by Tinkster; 05-20-2009 at 09:12 PM. Reason: poor grandma, errrrh, grammar
 
Old 05-20-2009, 07:20 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'FNR==NR{t[$1];next}($1 in t)' targets file
 
Old 05-20-2009, 08:49 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,533

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I'm not a big user of awk, but won't that only work if the pattern matches the full text - rather than a substring match as seems the requirement ?.
 
Old 05-20-2009, 09:00 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
I'm not a big user of awk, but won't that only work if the pattern matches the full text - rather than a substring match as seems the requirement ?.
Code:
FNR==NR{t[$1];next}
gathers all column 1 of "targets" file into array t. (note, the sample given has only 1 column. so essentially, we can also write :FNR==NR{t[$0];next}
Code:
($1 in t)
check $1 of test.txt against the array t. if found, print.
 
Old 05-20-2009, 09:12 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,533

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Thanks - like I said ...
 
Old 05-21-2009, 02:34 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Keep it simple:
Code:
while read pattern; do grep $pattern file; done < patterns
 
Old 05-21-2009, 02:59 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
Keep it simple:
Code:
while read pattern; do grep $pattern file; done < patterns
some issues with this, (assuming the samples as it is provided)
1)probably should put a boundary on the grep pattern, eg grep "target1" will also match target10 or target100 ....which will give ambiguous results
2)assuming "patterns" file has 1000 targets as OP has mentioned, so the code will call grep 1000 times on "file". it would be slower than going through the file once( as in the awk example). Its even slower if "file" is a big file.
 
Old 05-21-2009, 03:43 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
ghostdog, you're right as always. Anyway, I suspect the OP does not have GNU awk. I tested your code on a Solaris Sparc 5.8 and it doesn't work, due to a syntax error
Code:
$ awk 'FNR==NR{t[$1];next}($1 in t)' targets file
awk: syntax error near line 1
awk: bailing out near line 1
This is due to the last statement ($1 in t). Maybe we can slightly change it for portability. Anyway, let's wait for the OP reply. Cheers!
 
Old 05-21-2009, 04:04 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use nawk on Solaris
 
Old 05-21-2009, 06:14 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by ghostdog74 View Post
use nawk on Solaris
Correct! It works!
 
Old 05-24-2009, 02:01 PM   #15
mjtruco
LQ Newbie
 
Registered: May 2009
Posts: 3

Original Poster
Rep: Reputation: 0
You were right. I was using an old version of Solaris.

I've changed to a new version from GNU Free Software
Foundation and the command grep -f worked fine.

Thanks for the help

mjtruco
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 06:07 PM
grep multiple files Curtor Linux - Newbie 10 07-06-2008 01:32 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
automake and install of multiple targets therf Linux - Software 0 09-14-2005 04:08 PM
make with multiple targets eskimo22 Programming 1 02-26-2004 11:42 AM


All times are GMT -5. The time now is 07:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration