Using the same file for stdin and stdout with redirection

后端 未结 3 613
误落风尘
误落风尘 2020-12-30 04:56

I\'m writing a application that acts like a filter: it reads input from a file (stdin), processes, and write output to another file (stdout). The input file is completely re

3条回答
  •  半阙折子戏
    2020-12-30 05:48

    There's a sponge utility in moreutils package:

    ./myprog < file.txt | sponge file.txt
    

    To quote the manual:

    Sponge reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows constructing pipelines that read from and write to the same file.

提交回复
热议问题