sed - 删除匹配模式以外的所有单词

时间:2014-06-29 23:06:31

标签: regex bash awk sed grep

我需要在sed / awk / grep中删除除模式之外的所有单词的命令。

我有输入:

  

Charles Hatchett 是一名英国化学家,发现了元素

我需要以下输出:

  

Charles Hatchett发现了铌

我在google中找到了一些答案,但即使在sed或grep中我也无法加入所有这些命令。

1 个答案:

答案 0 :(得分:3)

通过awk,

$ echo 'Charles Hatchett was an English chemist who discovered the element niobium.' | awk '{print $1,$2,$8,$11}'
Charles Hatchett discovered niobium.

如果您最后不想.,请尝试

$ echo 'Charles Hatchett was an English chemist who discovered the element niobium.' | awk '{sub(/.$/,"",$11); print $1,$2,$8,$11}'
Charles Hatchett discovered niobium

OR

通过sed反向引用,

$ echo 'Charles Hatchett was an English chemist who discovered the element niobium.' | sed 's/.*\(Charles Hatchett\).*\(discovered\).*\(niobium\).*/\1 \2 \3/g'
Charles Hatchett discovered niobium

OR

通过sed删除一些单词以获得预期结果,

$ echo 'Charles Hatchett was an English chemist who discovered the element niobium.' | sed 's/was an English chemist who //g;s/the element //g;s/.$//g'
Charles Hatchett discovered niobium