Consider a file called .env containing:
env1=foo
env2=bar
I use grep with a regular expression to confirm there's a line defining env2 with a non-blank value, expecting to get a match.
~$ grep -c -i '^env2=(?!\s*$). ' .env
0
Returns 0 matches... but why? I got a match when I tested the same thing here: https://regexr.com/6g7of
Sanity check:
~$ grep -c -i '^env2=bar' .env
1
To confirm multiline is supported in case I had a doubt.
CodePudding user response:
Your ^env2=(?!\s*$). regex is a PCRE compliant regex, but you are using grep with the default POSIX BRE regex engine.
If you use a GNU grep, you can use the -P option to make grep treat the pattern as a PCRE regex:
grep -cPi '^env2=(?!\s*$). ' .env
Else, use a POSIX compliant pattern,`
grep -c -i '^env2=.*[^[:space:]]' .env
Here, the regex matches
^- start of stringenv2=- literal text.*- zero or more chars[^[:space:]]- a non-whitespace char.
