Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-14-2014, 01:08 AM   #1
Registered: Feb 2013
Posts: 274

Rep: Reputation: 9
grep -f example

Hi, I really have searched the web, but I have not yet found a good example to elaborate "grep -f" command. I would be very grateful if you kindly help me with that.
Old 10-14-2014, 07:49 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,353

Rep: Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036Reputation: 5036
see man page:
      -f FILE, --file=FILE
              Obtain patterns from FILE, one per line.  The empty  file  contains  zero  patterns,  and  therefore
              matches nothing.  (-f is specified by POSIX.)
I think it is quite simple. What causes your problem?
Old 10-14-2014, 08:04 AM   #3
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,976
Blog Entries: 13

Rep: Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089Reputation: 4089
Next time try playing around with the command a bit. Personally unless someone gives this highly involved, and more correct, answer, I think the use of this argument is near pointless, but that's just IMHO. After all, this answer is moderately involved ...

First, here's what grep tells you in the manual page:
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
Therefore my interpretation is that you could use -e to specify a pattern or use -f to specify a filename containing multiple patterns.

OK, actually had a txt file from answering a different persons question, the text content came from there so please don't inquire what these links are, they were never mine.

Original file, named myfile.txt:
I make a second copy named myfile1.txt and edited one of the lines to remove my intended search patterns:
The difference being that I removed "wavetlan" and "Media" from one of the lines in myfile1.txt.

And finally I created a text file to contain the search patterns, named search.txt:
The last action is then to use grep with -f specifying search.txt and looking for the wildcard myfile*.txt to look at both copies of the target text files:
grep -f search.txt my*.txt 
Only one line is found out of the myfile1.txt because neither of those patterns are seen in the second line. Both lines in myfile.txt are found because both lines contain the patterns. In my window, grep does colorize the results to highlight the patterns, similarly to how I've shown it here.

No harm in experimenting. I think "maybe" I might've needed something similar once in my near 30 years of work, because I recall once writing a script which needed to find patterns from a very extensive and varied list but that's about it.

The uniqueness here is that "both" patterns are found in an OR condition (I believe) versus if you did a grep and then piped the result to another grep, it would be an AND condition. That's about the only useful part I can see here.
1 members found this post helpful.
Old 10-14-2014, 08:43 AM   #4
Registered: Feb 2013
Posts: 274

Original Poster
Rep: Reputation: 9
@ rtmistler - Thanks indeed. You solved my problem about the file that contains pattern.


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 to file outputs more than grep to screen? tcpman Linux - Server 4 06-07-2013 04:46 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 02:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM > Forums > Linux Forums > Linux - Newbie

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

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