Regular expressions come in many different flavours. What you are showing is a Perl-like regular expression (PCRE, "Perl Compatible Regular Expression").
grep
does POSIX regular expressions. These are basic regular expressions (BRE) and extended regular expressions (ERE, if grep
is used with the -E
option). See the manual for re_format
or regex
or whatever similar manual your grep
manual refers to on your system, or the POSIX standard texts that I just linked to.
If you use GNU grep
, you would be able to use Perl-like regular expressions if you used grep
with the GNU grep
-specific -P
option.
Also note that grep
returns lines by default, not substrings from lines. Again, with GNU grep
(and some other grep
implementations), you may use the -o
option to get only the bit(s) that matches the given expression from each line.
Note that both -P
and -o
are non-standard extensions the POSIX specification of grep
.
If you are not using GNU grep
, then you may use sed
instead to get the bit between the string prefix
and the end of the line:
sed -n 's/.*prefix\(.*\)/\1/p' file
What this does is to only print the lines that sed
manages to apply the given substitution to. The substitution will replace the whole line that matches the expression (which is a BRE), with the piece of it that occurs after the string prefix
.
Note that if there are several instances of prefix
on a line, the sed
variation would return the string after the last one, while the GNU grep
variation would return the string after the first one (which would include the other instances of prefix
).
The sed
solution would be portable to all Unix-like systems.
.*$
matches any string up to the end-of-line (or end-of-string), not just any one character. – ilkkachu Feb 12 '19 at 17:24