简体   繁体   English

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

[英]Redirecting bash script output to a folder

I have two directories from which I am extracting specific columns from two files and saving them to a new 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) \
              <(cut "${b_files[i]}" -d, -f7-) > c_file"$i".csv

done

The code works, but I would like the output files to be saved in the output directory out_dir and to have the file names of the a_files 代码工作,但我想输出文件被保存在输出目录out_dir和具有的文件名a_files

I have tried to use >"out_dir/$a_files" but I get the error "No such files or directories". 我尝试使用>"out_dir/$a_files"但出现错误“无此类文件或目录”。

How can redirect the output files to a directory? 如何将输出文件重定向到目录?

I am using Linux Ubuntu. 我正在使用Linux Ubuntu。

Update: a_files and b_files have the same number of rows but they exist in different folders. 更新: a_filesb_files具有相同的行数,但是它们存在于不同的文件夹中。

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

But this so much feels for me like a job for xargs, but that's just me: 但这对我来说感觉非常像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")"' --

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM