代码之家  ›  专栏  ›  技术社区  ›  Buzz

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

  •  0
  • Buzz  · 技术社区  · 7 年前

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

    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
    

    out_dir a_files

    >"out_dir/$a_files" 但我得到的错误是“没有这样的文件或目录”。

    如何将输出文件重定向到目录?

    我用的是Linux和Ubuntu。

    更新: a\U文件 b_files 行数相同,但存在于不同的文件夹中。

    1 回复  |  直到 7 年前
        1
  •  2
  •   KamilCuk    7 年前
    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
    

    但我觉得这对xargs来说就像一份工作,但那只是我:

    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")"' --
    
    推荐文章