Results 1 to 3 of 3
Hi
Does any body know how to use the -f option of grep?
As far as i've understood from the man page each line is treated as a pattern
-f ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-30-2009 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 18
grep -f [SOLVED]
Hi
Does any body know how to use the -f option of grep?
As far as i've understood from the man page each line is treated as a patternFor trivial searches i get it working-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore
matches nothing.
Example:
This result is as expected. But when i want to use regular expressions in the pattern file it does not seem to work, so probably i am doing it wrong.Code:[jody ~]$ cat sample.txt line one second line another one number four [jody ~]$ cat pat.txt one line [jody ~]$ grep -f pat.txt sample.txt line one second line another one
Example:But if i put this pattern string into a file, it doesn't work.Code:[jody~]$ cat sample2.txt 5 6 a 55 6 b 5 665e c 4 67u d 1 1 e 1 2 f 5 4 g 15 6 h 5 62 i 5 6 j 7 82 k [jody ~]$ grep -e "^ *5 *6 * " sample2.txt 5 6 a 5 6 j
If i use it as above, all lines are returned, if i add a '-e', no lines are returned:
I also tried the '-e' inside the pattern file, but thenagain all lines are returned.Code:[jody ~]$ cat pat2.txt "^ *5 *6 " [jody ~]$ grep -f pat2.txt sample2.txt 5 6 a 55 6 b 5 665e c 4 67u d 1 1 e 1 2 f 5 4 g 15 6 h 5 62 i 5 6 j 7 82 k [jody ~]$ grep -e -f pat2.txt sample2.txt [jody ~]$
Does anybody know how to use this correctly?
Can one also specify to not return lines matching the pattern in the pattern file (like '-v')?
I need this because i have to filter out lines matching
different patterns from a large file.
Thank You
JodyLast edited by jody; 11-30-2009 at 09:37 PM. Reason: solved
- 11-30-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,148
Did you try leaving the double-quotes out of pat2.txt? I think it adds those to the pattern so since your sample2.txt file has no double-quotes in it, the pattern doesn't match.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-30-2009 #3Just Joined!
- Join Date
- Sep 2007
- Posts
- 18
Thank you, rubberman, that did it!
I can also exclude the patterns in pat2.txt by doing
Of course no quotes mean the pattern is a bit hard toCode:grep -v pat2.txt sample2.txt
read if there are spaces at the end, but that is only a minor thing.
Thanks
Jody


Reply With Quote
