Linux 命令将文件连接到自身 n 次

Bry*_*mas 36 linux concatenation

我从古腾堡项目(大约 0.5MB)中获取了一本纯文本文件书,我想将其连接到自身n时间以生成一个大文本文件,我可以对某些算法进行基准测试。是否有我可以使用的 linux 命令来实现此目的? cat听起来很理想,但将文件连接到自身上似乎不太好,而且没有直接解决问题的n时间部分。

Jou*_*eek 40

两部分,对我来说 - 首先 - 使用 cat 将文本文件输出到标准输出,并使用 append 将其添加到另一个文件 - 例如 foo.txt>>bar.txt 将 foo.txt 附加到 bar.txt

然后用它运行 n 次

for i in {1..n};do cat foo.txt >> bar.txt; done
Run Code Online (Sandbox Code Playgroud)

用您的号码替换该命令中的 n

应该可以工作,其中 n 是您的号码

如果您使用 csh,则有“repeat”命令。

答案的重复相关部分是从这里复制的,我在默认 bash shell 的 ubuntu 11.04 系统上对其进行了测试。

  • 有趣的事实:这实际上可以在不替换 'n' 的情况下工作,在这种情况下,它将为 ASCII '1' 和 ASCII 'n' 之间的每个字符执行一次主体(所以 62 次)。但是 `{1..12}` 会正确运行 body 12 次。 (3认同)
  • 您可能只想重定向整个管道,而不是在每次迭代中附加:`for i in {1..n};do cat foo.txt; 完成> bar.txt` (3认同)

Tob*_*ght 5

您当然可以cat为此使用:

$ cat /tmp/f
foo
$ cat /tmp/f /tmp/f
foo
foo
Run Code Online (Sandbox Code Playgroud)

要获取$n副本,您可以使用yespiped into head -n $n

$ yes /tmp/f | head -n 10
/tmp/f
/tmp/f
/tmp/f
/tmp/f
/tmp/f
/tmp/f
/tmp/f
/tmp/f
/tmp/f
/tmp/f
Run Code Online (Sandbox Code Playgroud)

把它放在一起给出

yes /tmp/f | head -n $n | xargs cat >/tmp/output
Run Code Online (Sandbox Code Playgroud)