为什么它适用于 sed -e 但不适用于 sed -i

why does it work with sed -e but not with sed -i

我在文件中有这个

echo "[[z[z[[e" > toto_test.txt

我正在尝试这个,一切正常

sed -e 's,\],,g' -e 's,\[,,g' toto_test.txt

这样做突然就不行了

sed -i 's,\],,g' -i 's,\[,,g' toto_test.txt

我有这个错误

sed: impossible to read s,\[,,g: No such file or directory

为什么以及如何克服这个问题?

谢谢。

您不需要更换 2 个。只需将其用作:

sed -i.bak 's,[][],,g' toto_test.txt

据我所知,-i 不能重复。它期望就地编辑的文件有一个可选的备份后缀(在 OS X 上是必需的)。

尝试

sed -i 's,\],,g; s,\[,,g' toto_test.txt

(想发表评论,但我似乎不被允许这样做,所以我把它作为一个答案)

尝试

sed -i -e 's,\],,g' -e 's,\[,,g' toto_test.txt

-i表示就地修改,

-e表示表达式。

一旦理解了这一点,就很容易提供适当的参数。