Results 1 to 3 of 3

Thread: "grep -f" usage

  1. #1
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    "grep -f" usage

    If I want to use "grep -f PATTERNS" to load patterns from a file, how do I tell it to ignore comments (lines that start with #) and blank lines from the PATTERNS file?

    Ex:

    Command:
    Code:
    $ grep -f PATTERNS /etc/passwd

    PATTERNS file:

    Code:
    sshd
    www-data
    list
    #The following is a blank line
    
    irc
    nobody
    #hplip
    While the 2 comments are technically not ignored, they do not match anything because the pattern "#hplip" and "#The following is a blank line" do not appear in /etc/passwd, making it not as big a problem (though explicitly ignoring commented out lines would be better).
    The blank line on the other hand matches everything. Without the blank line in the PATTERNS file, only 5 lines from /etc/passwd are returned. With the blank line, all the lines of /etc/passwd are returned! Is there any way to tell grep to ignore the blank lines in the PATTERNS file?

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,346
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: "grep -f" usage

    Code:
    grep -f <( sed -r '/^#|^\s*$/d' patterns.txt ) /etc/passwd
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Re: "grep -f" usage

    Quote Originally Posted by Vaphell View Post
    Code:
    grep -f <( sed -r '/^#|^\s*$/d' patterns.txt ) /etc/passwd
    Exactly what I was looking for - works awesome! Thanks!

Bookmarks

Posting Permissions

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