Reputation: 17025
Im trying to print everything after a keyword using grep but the command returns the whole line. Im using the following:
grep -P (\skeyword\s)(.*)
an example line is:
abcdefg keyword hello, how are you.
The result should be hello, how are you
but instead it gives the full line. Am I doing something wrong here?
Upvotes: 2
Views: 181
Reputation: 174756
You need to use -o
(only matching) parameter and \K
(discards the previously matched characters) or a positive lookbehind.
grep -oP '\skeyword\s+\K.*' file
\K
keeps the text matched so far out of the overall regex match. \s+
matches one or more space characters.
Example:
$ echo 'abcdefg keyword hello, how are you.' | grep -oP '\skeyword\s+\K.*'
hello, how are you.
Upvotes: 6
Reputation: 16361
By default, Grep prints lines that match. To print only matching expressions try the '-o' option.
Upvotes: 2