输出保存在与输入文件相同的文件中

yis*_*sha 5 shell grep

我有一个输入文件并运行一些命令,但希望以与输入文件相同的名称保存输出。

我尝试了下面的命令,但它使输出文件为空:

cat file1 | grep "YISHA" > file1
Run Code Online (Sandbox Code Playgroud)

cuo*_*glm 8

在 GNU 系统上,您可以使用sed(GNU 实现):

sed -i -n '/YISHA/p' file1
Run Code Online (Sandbox Code Playgroud)

FreeBSD 或 OS/X 等价物:

sed -i '' -n '/YISHA/p' file1
Run Code Online (Sandbox Code Playgroud)

或使用sponge来自moreutils:

grep "YISHA" file1 | sponge file1
Run Code Online (Sandbox Code Playgroud)

  • @yisha:你需要安装`moreutils`。 (2认同)