LinuxQuestions.org
Review your favorite Linux distribution.
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 10-14-2014, 02:08 AM   #1
sryzdn
Member
 
Registered: Feb 2013
Posts: 253

Rep: Reputation: 1
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, 08:49 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,491

Rep: Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429Reputation: 2429
see man page:
Code:
      -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, 09:04 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,466
Blog Entries: 10

Rep: Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639Reputation: 1639
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:
Quote:
SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
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:
Code:
http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4
http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test3_Talkingheadclipped_mp4_480x360.mp4
I make a second copy named myfile1.txt and edited one of the lines to remove my intended search patterns:
Code:
http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4
http://.com/SVV/HTTP/H264/Talkinghead/H264_test3_Talkingheadclipped_mp4_480x360.mp4
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:
Code:
wavetlan
Media
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:
Code:
grep -f search.txt my*.txt 
myfile1.txt:http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4
myfile.txt:http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4
myfile.txt:http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test3_Talkingheadclipped_mp4_480x360.mp4
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, 09:43 AM   #4
sryzdn
Member
 
Registered: Feb 2013
Posts: 253

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


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


All times are GMT -5. The time now is 11:07 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