我有一个多行文档,我希望从中提取一个特定的关键字和之后的单词.它看起来像这样:
This is key word1 line 1.
This is line 2.
This is key word2 line 3.
Run Code Online (Sandbox Code Playgroud)
如果我使用egrep 'key [^s]+ ',输出是:
This is key word1 line 1.
This is key word2 line 2.
Run Code Online (Sandbox Code Playgroud)
但是,我希望输出只是匹配而不是整行,即:
key word1
key word2
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
Car*_*rum 51
grep(1)有一个-o标志只输出该行的匹配部分.从手册页:
Run Code Online (Sandbox Code Playgroud)-o, --only-matching Show only the part of a matching line that matches PATTERN.
但是,您的模式不适合获得您想要的输出.尝试:
$ egrep -o 'key \w+' file
key word1
key word2
Run Code Online (Sandbox Code Playgroud)