我有n 个文件,每行一个字
文件 1 文件 2 文件 3 ... 1_a 2_a 3_a 1_b 2_b 3_b 1_c 3_c
我想编写一个 bash 脚本来获取所有这些文件并生成 n 个单词的所有可能组合(每个文件一个)。
在我的例子中,我想要这个结果:
1_a 2_a 3_a 1_a 2_a 3_b 1_a 2_a 3_c 1_a 2_b 3_a 1_a 2_b 3_b 1_a 2_b 3_c 1_b 2_a 3_a 1_b 2_a 3_b 1_b 2_a 3_c 1_b 2_b 3_a 1_b 2_b 3_b 1_b 2_b 3_c 1_c 2_a 3_a 1_c 2_a 3_b 1_c 2_a 3_c 1_c 2_b 3_a 1_c 2_b 3_b 1_c 2_b 3_c
我试图用粘贴和 awk 来做到这一点,但我失败了。我怎样才能做到这一点 ?
您可以使用递归函数在有文件要处理时调用自身:
#!/bin/bash
process () {
local prefix=$1
local file=$2
shift 2
while read line ; do
if (($#)) ; then # There are still unprocessed files.
process "$prefix $line" "$@"
else # Reading the last file.
printf '%s\n' "$prefix $line"
fi
done < "$file"
}
process '' "$@"
Run Code Online (Sandbox Code Playgroud)