I am running a script to merge multiple files (14) by columns, the script looks like the following:
cp $file_1 ${out_file}
for i in {2..14};do
paste -d ',' <(cat ${out_file}) <(cut -d ',' -f 2 ${file}_${i}) > ${out_file}
done
However, strangely, this script will always fail in the 1st run (missing row names), and succeed on a 2nd run. Any ideas what was going on here?
Surely, I can add a copy command to avoid using ${out_file}
twice, something like:
cp $file_1 ${out_file}
for i in {2..14};do
paste -d ',' <(cat ${out_file}) <(cut -d ',' -f 2 ${file}_${i}) > ${out_file}.new
cat ${out_file}.new > ${out_file} #This line
done
But is there a better way to do this without copying files in every iteration?
======Here are some details======
file #1:
Name,Simon
Age,30
Sex,Male
Weight,150
file #2:
Name,Mary
Age,27
Sex,Female
Weight,120
I want to merge them to:
Name,Simon,Mary
Age,30,27
Sex,Male,Female
Weight,150,120
If I only run the script once, I will get:
,Simon,Mary
,30,27
,Male,Female
,150,120
After I re-run the script, I will get the correct output:
Name,Simon,Mary
Age,30,27
Sex,Male,Female
Weight,150,120