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?
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.
By default, Grep prints lines that match. To print only matching expressions try the '-o' option.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.