What's wrong with this grep?

5,622

Your issue is the -f option. Instead of specifying the file to search, -f specifies a file to read a list of patterns from. OS X grep's man page explains it, though not very clearly:

 -f file, --file=file
         Read one or more newline separated patterns from file.  Empty pattern lines match every input
         line.  Newlines are not considered part of a pattern.  If file is empty, nothing is matched.

The help for GNU grep is actually more straightforward:

$ grep --help | grep -- '-f,'
  -f, --file=FILE           obtain PATTERN from FILE
$ 

This behavior of -f is, according to GNU grep's man page, specified by POSIX.

Your fix is probably to change your line:

grep -f $line $regex

to:

egrep "$regex" -- "$line"
  • You are using an extended regular expression so use egrep or grep -E
  • The -- will prevent grep from parsing any options in the $line variable, e.g. it would protect you against a file named "-r funnyname.js"
Share:
5,622

Related videos on Youtube

itsmichaelwang
Author by

itsmichaelwang

Programmer in San Francisco and enjoying life. Open to side work. Hmu.

Updated on September 18, 2022

Comments

  • itsmichaelwang
    itsmichaelwang over 1 year
    > output2.txt
    cd # some directory i'm trying to search
    find views/shared -type f -name "*.js" -print0 | while IFS= read -r -d $'\0' line; do
        echo -n "${line%.js}" | tee -a ~/Documents/counter/output2.txt
        grep -lr "${line%.js}" . | wc -l | tee -a ~/Documents/counter/output2.txt   # produce a count of occurrences
        regex='[a-zA-Z]+.extend'
        grep -f $line $regex
        grep -lr "${line%.js}" . | tee -a ~/Documents/counter/output2.txt           # produce a list of occurrences
    done
    

    Returns

    grep: brackets ([ ]) not balanced
    

    All the examples I've seen on the web seem to indicate there is nothing wrong here, so i'm pretty confused

    Surely the square brackets are balanced, aren't they?

    • Jakuje
      Jakuje almost 9 years
      What are you trying to achieve? what is in your file referenced by $line variable? By default grep needs to escape brackets to take into effect, unless you use extended grep
    • itsmichaelwang
      itsmichaelwang almost 9 years
      I put the full command. I'm grepping through a BackboneJS code base and looking for the phrase "*.extends" to find parent classes
    • itsmichaelwang
      itsmichaelwang almost 9 years
      The brackets are for range selection
    • Jakuje
      Jakuje almost 9 years
      @drewbenn is right. You are using -f wrongly. And from your code is still not much obvious what you want to achieve.
  • itsmichaelwang
    itsmichaelwang almost 9 years
    So I'm not actually getting anything now... I want to search for the file's contents, and $line is the filename I believe. Is there a way to do that?
  • itsmichaelwang
    itsmichaelwang almost 9 years
    Actually I should probably clarify. I want to find this line: return BaseView.extend({ Everything is telling me the regex should be a match, but grep returns nothing.