删除所有包含 $PWD 的行

Ema*_*sev 1 text-processing variable pwd

我有一个包含路径的文件 - 看起来像这样:

/Users/a/Desktop
/Users/a/Documents
/Users/a/Documents/Work
Run Code Online (Sandbox Code Playgroud)

删除包含当前目录 ( $PWD) 的所有行的最简单方法是什么?

noh*_*ide 5

grep -v "^$PWD$" FILE-LIST
Run Code Online (Sandbox Code Playgroud)
  • -v 反转搜索,所以只打印不匹配的行
  • ^...$确保模式只匹配整行(否则所有子目录$PWD也会被过滤)

  • 另请参阅为您执行 ^ 和 $ 的 -x 选项 (3认同)