我有一个大文件,我将大文件分成几个小块并分而治之。现在我有一个文件夹,其中包含如下文件列表:
output_aa #(the output file done: cat input_aa | python parse.py > output_aa)
output_ab
output_ac
output_ad
...
我想知道有没有办法按照索引顺序将这些文件重新合并在一起:
我知道我可以通过使用
cat * > output.all
但我更好奇另一个神奇的命令已经存在与分裂..
神奇的命令是:
cat output_* > output.all
无需对文件名进行排序,因为 shell 已经这样做了 (*)。
As its name suggests, cat original design was precisely to conCATenate files which is basically the opposite of split.
(*) Edit:
Should you use an (hypothetical ?) locale that use a collating order where the a-z order is not abcdefghijklmnopqrstuvwxyz, here is one way to overcome the issue:
LC_ALL=C "sh -c cat output_* > output.all"
还有其他方法可以将文件连接在一起,但是“linux”中没有神奇的“拆分相反”。
当然,一般来说谈论“linux”有点牵强,因为许多发行版都有不同的工具(其中大多数已经默认使用不同的 shell,如 sh、bash、csh、zsh、ksh ......),但如果你至少在谈论基于 debian 的 linux,我不知道有任何发行版会提供这样的工具。
对于排序,您可以使用 linux 命令 "sort" ;
另请注意,使用“>”重定向标准输出可能会覆盖现有内容,而“>>”将连接到现有文件。
我不想模仿,但仍然使这个答案完整,所以 jlliagre 关于 cat 命令的说法当然也应该考虑(“cat”被制作为 con-“cat”文件,有效地使得可以反转split 命令 - 但这仅提供您使用相同的文件顺序,因此它不完全是“与拆分相反的”,但在接近 100% 的情况下都会以这种方式工作(有关详细信息,请参阅 jlliagre 答案下的评论))