gpt4 book ai didi

performance - 将排序的文件与 fifos 组合

转载 作者:行者123 更新时间:2023-11-29 09:05:13 25 4
gpt4 key购买 nike

我在目录中有一些经过排序的 gzip 文件。我如何将其中一些组合到另一个排序的 gzip 文件中?现在我正在使用显式 fifos。有没有办法在 bash 中做到这一点?我有点 bash 菜鸟,所以请原谅我缺乏风格。

#!/bin/bash
# Invocation ./merge [files ... ]
# Turns an arbitrary set of sorted, gzipped files into a single sorted, gzipped file,
# printed to stdout. Redirect this script's output!
for f in $@
do
mkfifo $f.raw
gzcat $f > $f.raw &
# sort -C $f.raw
done
sort -mu *.raw | gzip -c # prints to stdout.
rm -f *.raw

我希望将其转换成类似...

sort -mu <(gzcat $1) <(gzcat $2) <(gzcat $3) ... | gzip -9c # prints to stdout.

...但不知道如何。我需要一个循环将参数构建为字符串吗?这有某种神奇的捷径吗?也许 map gzcat $@

注意:每个文件都超过 10GB(解压后超过 100GB)。我有一个 2TB 驱动器,所以这不是真正的问题。此外,该程序必须在 O(n) 内运行,否则将变得不可行。

最佳答案

您可以将 eval 和“进程替换”与 Bash 结合使用。假设基本文件名不包含空格(假设您使用 $@ 而不是 "$@" 可能就是这种情况),那么类似于:

cmd="sort -mu"
for file in "$@"
do cmd="$cmd <(gzip -cd $file)"
done
eval $cmd | gzip -c9 > outputfile.gz

您也可以在最后一行使用 bash -c "$cmd" 代替 eval $cmd。如果文件名中有空格,你就得加把劲了。如果名称不包含单引号,则此方法有效:

cmd="sort -mu"
for file in "$@"
do cmd="$cmd <(gzip -cd '$file')"
done
eval $cmd | gzip -c9 > outputfile.gz

文件名中也有单引号,您必须更加努力。

关于performance - 将排序的文件与 fifos 组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6326685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com