Skip to content
Advertisement

Pcregrep include file extension bug

I’m using pcregrep to search for a multiline pattern, and I only want pcregrep to search through files with specific file extensions, i.e.

pcregrep -Mrl --include=*.sv -e '<my_multi-line_pattern>' /path/to/search

However, this throws an error: pcregrep: Error in 'include' regex at offset 0: nothing to repeat.

I’ve tried escaping & double-escaping the * to no avail. This syntax seems to work just fine for grep

grep -rl --include=*.sv '<my_single-line_pattern>' /path/to/search

Any help or hints greatly appreciated.

EDIT: Example multi-line pattern: '(?s)^salu.*.opa_i(' should match

alu u_alu(
     ...
   .opa_i(opa),
   .opb_i(opb),
     ...
)

Advertisement

Answer

Here’s what pcregrep’s docs say about --include (emphasis mine):

--include=pattern If any --include patterns are specified, the only files that are processed are those that match one of the patterns (and do not match an --exclude pattern). This option does not affect directories, but it applies to all files, whether listed on the command line, obtained from --file-list, or by scanning a directory. The pattern is a PCRE regular expression, and is matched against the final component of the file name, not the entire path. The -F, -w, and -x options do not apply to this pattern. The option may be given any number of times. If a file name matches both an –-include and an --exclude pattern, it is excluded. There is no short form for this option.

And a * at the start of a regex is of course not valid.
Now you can make sense of the error message 🙂

Solution: --include='.*.sv$' or just --include='.sv$'

Advertisement