0

I am reading the man page, and found this blog post, and both seem to say the same thing, but it does not work.

I have a project where I need to batch replace lines like

import Foo from '/modules/foo/client/components/Foo.jsx';

into

import Foo from '/modules/foo/client/imports/Foo.jsx';

but I do not want to match lines like :

import Base from '/client/components/Base.jsx';

i.e. : only "imports" from the base dir /modules.

I have tried

$ grep -R "modules.+components" modules/

but no match is found.

Any help with this would be appreciated. Thanks!

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • you wrote that you need to *replace lines* but `grep` will only search for matches. So, what is your final goal? – RomanPerekhrest Oct 27 '17 at 15:40
  • I use grep all the time, but always searching one keyword at a time. I just want to make sure that I don't miss anything, as a verification tool. – Yanick Rochon Oct 27 '17 at 15:43

1 Answers1

1

By default, grep interprets the pattern as a basic regular expression (BRE) (not extended -E) and treats some regex metacharacters literally. So + doesn't have special meaning in your current approach.

Any meta-character with special meaning may be quoted by preceding it with a backslash.

grep -R "modules.\+components" modules/

https://www.gnu.org/software/grep/manual/grep.html#index-grep-programs


About BRE: https://www.regular-expressions.info/posix.html#bre

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • 1
    @YanickRochon, good thought and yes ... it *treats some regex metacharacters literally* cause in BRE `.` also matches any character, including newline, while `+` should be escaped `\+` – RomanPerekhrest Oct 27 '17 at 15:58