为什么我的输出重定向不写入文件?

Why does my output redirection not write to file?

我在名为 columns 的文件中包含以下内容:

  SCHEMA_123 | user                      | id
  SCHEMA_123 | user                      | name
  SCHEMA_123 | user                      | role

我 运行 以下内容并在 stdout 中获取我要查找的内容:

cat columns | sed 's/ //g' | sed 's/|/./g'

输出:

SCHEMA_123.user.id
SCHEMA_123.user.name
SCHEMA_123.user.role

我试图将输出重定向到具有相同文件名(列)的文件,但该文件最终为空。这是我正在使用的:

cat columns | sed 's/ //g' | sed 's/|/./g' > columns

如果我使用不同的文件名,我会得到我正在寻找的输出。我是否无法读取文件、修改文件并将其写回相同的文件名?

您不能读取和写入同一个文件,因为 shell 甚至在执行命令行之前会将输出文件截断为零字节。

sed中使用内联编辑选项:

sed -i.bak 's/ //g; s/|/./g' columns