is GNU ERE syntax (assuming you want
\s to match any spacing character, and
+ to match one or more of the preceding atom), so you'd need the GNU implementation of
grep and pass the
However, even then that wouldn't make much sense
is functionaly equivalent to
Because if a string matches
anything.+, then it also matches
anything. and vis-versa.
Also a spacing character is also a character. Since
\s* matches 0 or more spacing characters,
key\s*=\s*. is functionaly equivalent to
key\s*=. (lines that contain
Here you want:
to ask for at least one non-spacing character to be found on the right of the
=, which is functionaly equivalent to:
Note that it matches
key = foo but also
nonkey = foo. If you want the
key to be only found at the beginning of the line, you need to ask for it with the
-x for the regexp to match the whole line:
grep -x 'key\s*=.*\S.*'
Note that the standard equivalent of
Another way to address the requirement would be to use extended operators found in some regexps like the PCRE ones to prevent back-tracking.
key= because the regexp engine has
\s* go greedily through the space characters after the
=, finds 1 and then realises it can't match the
. as it reached the end of the line, and then back-tracks to try with fewer matches of
\s (0 in that case) so the next
. can match (here a space character).
With PCRE, like when using the
-P option with GNU
grep, you can write:
grep -P '^key\s*=(?>\s*).'
(?>...) syntax prevents back-tracking. So the
\s* will eat as many spacing characters as possible without being able to backtrack, so will only match if there's at least one non-spacing character after the spaces.
$ printf 'key=%s\n' '' ' ' ' a' | grep '^key\s*=\s*.'
$ printf 'key=%s\n' '' ' ' ' a' | grep -P '^key\s*=(?>\s*).'
$ printf 'key=%s\n' '' ' ' ' a' | grep '^key\s*=.*\S'