Buzz
Buzz

Reputation: 516

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_dirand to have the file names of the a_files

I have tried to use >"out_dir/$a_files" but I get the error "No such files or directories".

How can redirect the output files to a directory?

I am using Linux Ubuntu.

Update: a_files and b_files have the same number of rows but they exist in different folders.

Upvotes: 0

Views: 2131

Answers (1)

KamilCuk
KamilCuk

Reputation: 141768

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:

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")"' --

Upvotes: 2

Related Questions