Linux command
sponge 命令
文件
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Overwrite file with pipeline output
cat [file.txt] | sort | sponge [file.txt]
Filter and save in place
grep "[pattern]" [file.txt] | sponge [file.txt]
Transform and overwrite
sed 's/old/new/g' [file.txt] | sponge [file.txt]
Append instead of overwrite
echo "new line" | sponge -a [file.txt]
说明
sponge reads standard input and writes to a file. Unlike shell redirection, it reads all input before opening the output file, allowing safe in-place modifications. The tool is part of moreutils and solves the problem of using a file as both input and output in a pipeline.
参数
- -a
- Append to file instead of overwriting.
FAQ
What is the sponge command used for?
sponge reads standard input and writes to a file. Unlike shell redirection, it reads all input before opening the output file, allowing safe in-place modifications. The tool is part of moreutils and solves the problem of using a file as both input and output in a pipeline.
How do I run a basic sponge example?
Run `cat [file.txt] | sort | sponge [file.txt]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -a do in sponge?
Append to file instead of overwriting.