1

Trying to identify the difference between the two methods.

I recently had to find a the string abcABC in ~/path/. In that same directory were the subdirectories ~/path/files/ and ~/path/docs/ along with a series of .h, .cpp and .txt files. I only needed to look in the ~/path/ level so using:

grep -r -i abcABC ~/path/ produced findings in subdirectories which I didn't want.

Using the * wildcard to extend the path to be ~/path/*.*/ allowed me to use:

grep -i abcABC ~/path/*.* which found the subdirectories and files. However, I needed further refinement to only look at the .h and .cpp files for abcABC.

Did some reading in man grep and learned about --include=GLOB and altered my command to the following:

grep -i --include \*.h --include \*.cpp abcABC ~/path/*.*

This worked, but then I tried (for S&G's) the following:

grep -i abcABC ~/path/*.{h,cpp} and produced the same results I needed.

What I would like to know is what does { } do for grep? man grep puts {n,m} in repetition but I am not sure how doing this method equates to --include=GLOB.

EDIT: Last response by Jordan gives great detailed Wikipedia example. The duplicate (slightly) problem provides great use case.

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • `{}` doesn't do anything for grep. Your shell expands it before sending the arguments to grep. The reason you see a `\ ` in `--include \*.h` (for example) is to escape `*` so your shell doesn't expand it. – Jordan Running Jan 14 '17 at 17:16
  • Thanks for the reply! I knew that `\` escapes the `*` so that it no longer makes it special and turns it into a regex. If `{ }` isn't specific to `grep`, then is there a name for it? Is this `GLOB` or "globbing"? What is the functionality? – pstatix Jan 14 '17 at 17:35
  • https://en.m.wikipedia.org/wiki/Glob_(programming) – Jordan Running Jan 14 '17 at 17:39
  • Read that early but saw no mention of the `{ }` metacharacters. Curious to know what they functionality is, are they simply a bin for a range of options? – pstatix Jan 14 '17 at 17:41
  • 1
    "Some shells (such as the C shell and Bash) support additional syntax known as alternation or [brace expansion](https://en.m.wikipedia.org/wiki/Brace_expansion)." – Jordan Running Jan 14 '17 at 17:45
  • ... e.g., [this answer](https://stackoverflow.com/a/2188369/2877364). – cxw Jan 14 '17 at 18:08

0 Answers0