打印具有匹配模式的单词

时间:2014-10-17 19:52:07

标签: regex linux awk sed grep

我想在Linux中使用grep / sed / awk / etc打印具有匹配模式的单词。  我检查了grep / sed / awk所有的打印行,我只想要文件中的单词。

示例:

我的文件test.txt包含以下内容:

root@root:~]#cat test.txt
Notification are enabled Notification:445 Mode: valid Bookmark are enabled Bookmarks:556    Mode: Invalid Question are enabled Question:667 Mode: Unknown

然后我想只打印具有匹配模式*的单词

Notification
Notification
Question
Question

有没有办法在命令行中执行此操作?

2 个答案:

答案 0 :(得分:3)

您可以尝试使用GNU grep:

 grep -oE "\w*tion" test.txt

输出:

Notification
Notification
Question
Question

答案 1 :(得分:1)

您可以使用此awk命令:

awk -F '[: ]' 'index($1, "tion") {print $1}' test.txt
Notification
Notification
Question
Question