将bash脚本输出重定向到文件夹

将bash脚本输出重定向到文件夹,bash,file,Bash,File,我有两个目录,从中提取两个文件中的特定列并将其保存到新文件: shopt -s nullglob a_files=(/path/to/a_files/*.csv) b_files=(/path/to/b_files/*.csv) out_dir=(/path/to/output/folder) for ((i=0; i<"${#a_files[@]}"; i++)); do paste -d, <(cut "${a_files[i]}" -d, -f1-6) \

我有两个目录,从中提取两个文件中的特定列并将其保存到新文件:

shopt -s nullglob
a_files=(/path/to/a_files/*.csv)
b_files=(/path/to/b_files/*.csv)
out_dir=(/path/to/output/folder)

for ((i=0; i<"${#a_files[@]}"; i++)); do
    paste -d, <(cut "${a_files[i]}" -d, -f1-6) \
              <(cut "${b_files[i]}" -d, -f7-) > c_file"$i".csv

done
shopt-s nullglob
a_files=(/path/to/a_files/*.csv)
b_文件=(/path/to/b_文件/*.csv)
out_dir=(/path/to/output/folder)
对于((i=0;i
a_files=(/path/to/files/*.csv)
b_files=(/path/to/files/*.csv)
out_dir=“/path/to/output/folder”
#创建输出目录
mkdir-p“$out\u dir”
对于((i=0;i
a_files=(/path/to/files/*.csv)
b_files=(/path/to/files/*.csv)
out_dir=“/path/to/output/folder”
#创建输出目录
mkdir-p“$out\u dir”

对于((i=0;i目录是否存在?重定向将创建文件,但不会创建目录。另外,
a_文件
b_文件
是否真的相同?如果是,您不需要两者,是吗?
$a_文件
${a_文件[0]相同)
,它是一个完整的路径,就像
/path/to/files/foo.csv
,而不仅仅是
foo.csv
@BenjaminW。我假设OP使用“/path/to/files”可以理解为对他实际路径的混淆,而不是指示a_文件和b_文件实际上在同一个目录中……然而,这种混淆会阻止其他人复制g问题,使问题与规则冲突。问题中的代码应该是最短的东西,可以在提出问题之前成功测试,以在所有基本方面重现问题。目录是否存在?重定向将创建文件,但不会创建目录。此外,还有a_文件b_文件
真的一样吗?如果是这样,您不需要两个文件,是吗?
$a_文件
${a_文件[0]}相同
,它是一个完整的路径,就像
/path/to/files/foo.csv
,而不仅仅是
foo.csv
@BenjaminW。我假设OP使用“/path/to/files”可以理解为对他实际路径的混淆,而不是指示a_文件和b_文件实际上在同一个目录中……然而,这种混淆会阻止其他人复制g问题,使问题与规则冲突。问题中的代码应该是最短的东西,可以在提出该问题之前成功测试,以在所有基本方面重现问题。有趣的使用xargs,但join-z解决方案似乎是维护方面的难题;-)只是MHO。祝大家好运。这解决了我的问题。xargs选项也起作用,但我已经确定了第一个答案。谢谢大家。有趣地使用了
xargs
,但是
join-z
解决方案似乎是一个维护难题;-)只是MHO。祝大家好运。这解决了我的问题。xargs选项也有效,但我已经确定了第一个答案。谢谢大家。
a_files=(/path/to/files/*.csv)
b_files=(/path/to/files/*.csv)
out_dir="/path/to/output/folder"

# create the output directory
mkdir -p "$out_dir"
for ((i=0; i<"${#a_files[@]}"; i++)); do
    # move the output to "$out_dir" with the filename the same as in ${a_files[i]}
    paste -d, <(cut "${a_files[i]}" -d, -f1-6) <(cut "${b_files[i]}" -d, -f7-) \
      > "$out_dir"/"$(basename "${a_files[i]}")"
done
a_path="/path/to/files/*.csv"
b_path="/path/to/files/*.csv"
out_dir="/path/to/output/folder"

join -z <(printf "%s\0" $a_path) <(printf "%s\0" $b_path) | xargs -0 -n2 sh -c 'paste -d, <(cut "$1" -d, -f1-6) <(cut "$2" -d, -f7-) > '"$out_dir"'/"$(basename "$1")"' --