是否可以直接将输出文件与输入文件同名,即覆盖?
由于系统中的磁盘空间有限,我想用与输出文件相同的名称覆盖输入文件的名称。是否可以?我知道不建议这样做,但我已经备份了输入文件。我将在 shell 中创建一个循环来执行 cut 命令。
#!/bin/bash
for i in {1..1000}
do
cut --delimiter=' ' --fields=1,3-7 input$i.txt > input$i.txt
done
I would like to overwrite the name of input file with the same name of output file owing to limited disk space that I have in my system. Is it possible? I know this is not recommended, but I have the input files already backup. I will have a loop in a shell to do the cut command.
#!/bin/bash
for i in {1..1000}
do
cut --delimiter=' ' --fields=1,3-7 input$i.txt > input$i.txt
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您始终可以使用重定向到的临时文件,然后当您确定一切正常时,将其重命名为原始文件。
You could always use a temporary file to which you redirect, and then when you're sure everything went fine, you rename it to the original file.
一些 gnu utils 命令有一个 -i 选项(例如 sed),允许您就地更改文件......大多数文件过滤和编辑(例如剪切)可以使用 sed 完成。
some gnu utils commands have a -i option (such as sed) that allow you to change a file in place .....most of file filtering and editing (like cut) can be done using sed.
shell 将首先解析命令并处理重定向。当它看到“> afile”时,它将截断“afile”并打开它进行写入。您的数据现已被销毁。 然后 shell 将文件名交给
cut
,现在没有任何内容可读取。这就是我了解到的:
尽可能长时间地保留原始数据。
如果您遇到磁盘空间问题,则必须将输入文件完全读入内存。
The shell will parse the command and handle the redirections first. When it sees "> afile" it will truncate "afile" and open it for writing. Your data is now destroyed. Then the shell hands the filename to
cut
which now has nothing to read.This is how I learned:
That keeps the original data in place for as long as possible.
If you're having disk space issues, you will have to read the input file into memory entirely.
如果磁盘空间非常有限(磁盘配额),您可以尝试将压缩源文件放入 ram (
/dev/shm
) 中,并将其用作源(将其解压缩到 stdout 并将其通过管道传输到你的脚本)。In case of very limited disk space (disk quota) you could try to place a compressed source file in ram (
/dev/shm
) and use that as the source (uncompressing it to stdout and piping that to your script).