Find the answer to your Linux question:
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.
  1. #1
    Just 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 pattern
    -f FILE, --file=FILE
    Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore
    matches nothing.
    For trivial searches i get it working
    Example:
    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
    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.
    Example:
    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
    But if i put this pattern string into a file, it doesn't work.
    If i use it as above, all lines are returned, if i add a '-e', no 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 ~]$
    I also tried the '-e' inside the pattern file, but thenagain all lines are returned.

    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
    Jody
    Last edited by jody; 11-30-2009 at 09:37 PM. Reason: solved

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,512
    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!

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    18
    Thank you, rubberman, that did it!
    I can also exclude the patterns in pat2.txt by doing
    Code:
    grep -v pat2.txt sample2.txt
    Of course no quotes mean the pattern is a bit hard to
    read if there are spaces at the end, but that is only a minor thing.

    Thanks
    Jody

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •