首页 > 解决方案 > 当我没有文件但只有一行的长字符串时,如何在 grep 中使用反转“-v”?

问题描述

假设我有

echo "The first part. The second part. The third part."

并希望删除The first partThe third part获得:

The second part.

我试过了:

echo "The first part. The second part. The third part." | grep -v -e "The first part." -e "The third part."

但是反转标志似乎仅适用于具有多行的文件。我怎样才能为单个字符串做到这一点?

标签: linuxgrep

解决方案


改用sed

echo "The first part. The second part. The third part." \
| sed -e 's/[[:space:]]*The first part\.[[:space:]]*//g' \
      -e 's/[[:space:]]*The third part\.[[:space:]]*//g'

推荐阅读