2

I'm trying to figure out how to use grep to search for for a specific string, (for the sake of simplicity) say "9", and I want to search for this, again for simplicity, on all files containing the character b.

I tried grep -e "9" b, but it says it can't find file b. (The same if I don't use the -e option).

I know that I could go for grep -e "9" *b*, but that's not my point. I want the usual tools of regular expressions to use for filename matching.

I'm not interested in convoluted ways of obtaining this. I'm hoping there is a simple option in grep that I can use for this. I perused the man and I can't find anything useful, but I'm pretty new to shell scripting, so I could very easily be missing it.

Is it possible to use regular expression for filename matching using just grep? If so, how?

  • With the command grep -e "9" *b* you use usual regex to match filename (and content of the file) – Romeo Ninov Dec 24 '18 at 16:48
  • 1
    @RomeoNinov "I know that I could go for grep -e "9" *b*, but that's not my point". I have no reason to believe this is anything other than simply using the wildcard *. I'm asking about being able to use the whole typical regexs tool belt. For instance grep -e "9" ^b* doesn't work. – newbie Dec 24 '18 at 16:51
  • Depending on your grep implementation, you may be able to use its --include and/or --exclude options - however these use glob matching, rather than regular expressions – steeldriver Dec 24 '18 at 16:53
  • 1
    Combine it with ls (for example) and you can have the full spread of regex – Romeo Ninov Dec 24 '18 at 16:53
  • @RomeoNinov I edited my question to eliminate that possibility. I was including that in the "convoluted ways". (Please understand that I'm not in a position to judge what's convoluted or not, given that I'm just starting with shell scripting - apologies for not being clear). – newbie Dec 24 '18 at 16:57
2

There is no option in the grep itself to match files using regexp.

You have two options:

  1. Use shell glob, like

    grep -e '9' b*
    

    The above is very simple example, but you can match really fancy filenames, especially if you use some of the extension mechanisms. Bellow is a list of some useful bash's feature which can be switched on, similar can be found in zsh and possible other shells:

    • globstar (uses ** to match whole directory tree)
    • nocaseglob (case-insensitive match)
    • extglob (extended patterns: ?(), @(), !(), *(), +())
    • nullglob (non-matching patterns expand to null string)
  2. Use find to search for files and on execute grep from it:

    find . -name 'b*' -exec grep '9' {} +
    

    or if regex is needed:

    find . -regex './b.*' -exec grep '9' {} +
    

    The advantage of this method is that you can add another switches to find to select files not only by names but also by dates, sizes, etc. That cannot be done with standard shell's globs unless you are using zsh and its powerful glob qualifiers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.