是否可以直接将输出文件与输入文件同名,即覆盖?

发布于 2024-10-06 16:31:10 字数 231 浏览 2 评论 0原文

由于系统中的磁盘空间有限,我想用与输出文件相同的名称覆盖输入文件的名称。是否可以?我知道不建议这样做,但我已经备份了输入文件。我将在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还如梦归 2024-10-13 16:31:10

您始终可以使用重定向到的临时文件,然后当您确定一切正常时,将其重命名为原始文件。

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.

魔法唧唧 2024-10-13 16:31:10

一些 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.

尐籹人 2024-10-13 16:31:10

shell 将首先解析命令并处理重定向。当它看到“> afile”时,它将截断“afile”并打开它进行写入。您的数据现已被销毁。 然后 shell 将文件名交给 cut,现在没有任何内容可读取。

这就是我了解到的:

some | pipeline < my_file > my_file.tmp
ln my_file my_file.bak   # this is a hard link
mv my_file.tmp my_file

尽可能长时间地保留原始数据。

如果您遇到磁盘空间问题,则必须将输入文件完全读入内存。

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:

some | pipeline < my_file > my_file.tmp
ln my_file my_file.bak   # this is a hard link
mv my_file.tmp my_file

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.

谢绝鈎搭 2024-10-13 16:31:10

如果磁盘空间非常有限(磁盘配额),您可以尝试将压缩源文件放入 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文