【问题标题】:How to merge and delete the files in unix?如何在unix中合并和删除文件?
【发布时间】:2017-12-15 03:32:33
【问题描述】:

我想在 bash 中将多个文件合并为一个文件。所以我使用代码,

cat 文件1 文件2 文件3 文件4 >> 输出

但由于我的计算机内存不足,我无法创建合并文件。相反,您知道如何在将数据添加到输出文件后同时删除 file1 file2 file3 吗?

【问题讨论】:

    标签: bash unix cat


    【解决方案1】:

    我能想到的最好的是:

    mv file1 output
    cat file2 >> output && rm file2 &&
    cat file3 >> output && rm file3 
    

    这里的rms 几乎是最早有用的机会。

    当然,您可以在 for 循环中执行此操作。

    mv file1 output
    for f in file*; do
       cat $f >> output && rm $f
    done
    

    【讨论】:

      【解决方案2】:
      for i in file1 file2 file3 file4 ; do cat "$i" >> output && rm "$i" || break ; done
      

      所以对于每个文件,它会将内容附加到output,并在附加后删除源文件。如果出现任何问题,它就会停止。

      【讨论】:

        猜你喜欢
        • 2016-11-25
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        • 2016-11-17
        • 2012-06-02
        相关资源
        最近更新 更多