首页 > 解决方案 > sed:保留包含块模式的每个单词的最后一块

问题描述

愿意只保留最后一块包含点的单词(仍然保留不包含模式的单词):

sed 似乎只匹配最后一个单词:

$ echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " | sed 's/.*[ ]\([^ ]*\.\)\([^ ]*\)[ ].*/\2/g'
end

$ echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " | sed 's/.*[ ]\([^ ]*\.\)\([^ ]*\)[ ].*/\1/g'
that.is.the.

我该怎么做才能得到这个结果?:

echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " | sed s\\\g
Here start is a String  end yes

标签: shellsed

解决方案


用 \w 表示一个词:

echo "Here this.is.a.start is a very.nice.String  that.is.the.end yes " |
 sed -E 's/\w*\.//g'

推荐阅读