bash - How to combine some files with different sizes into one excel file using shell? -


i have these files, , want merge them in excel file each file have 2 columns

file1

title1  1 1   2 2   3 3 

file2

title2  5 5  6 6   7 7  8 8   9 9  10 10 

file 3

title3 21 21 22 22 23 23 24 24 

i use command below merge them in excel file

paste file* > out.csv 

the normal output ok,but when put in excel file(out.csv), output become this

title1 title2 title3 1  1   5  5   21  21 2  2   6  6   22  22 3  3   7  7   23  23    8  8   24  24    9  9   10 10 

but i want output

title1 title2 title3 1  1   5  5   21  21 2  2   6  6   22  22 3  3   7  7   23  23        8  8   24  24        9  9         10 10 

each number should in separate cells, separate columns , below each other when size of file longer previous file, additional numbers go last column , below previous file numbers, example numbers 8,9,10 , 24

you can try following script:

#!/bin/bash maxlines=$(wc -l file1 file2 file3 | grep -v total | sort -nr | head -1 | awk '{print $1}') f in file1 file2 file3     sed 's/ /,/g' "$f" > "$f".tmp     linecount=$(wc -l < $f.tmp)     linestoadd=$((maxlines-linecount))     ((i=0;i<linestoadd;i++))             echo "," >> "$f".tmp     done done paste -d, file{1,2,3}.tmp | sed '1 s/,/,,/g' rm file*.tmp 

output:

$ myscript.sh title1,,title2,,title3 1,1,5,5,21,21 2,2,6,6,22,22 3,3,7,7,23,23 ,,8,8,24,24 ,,9,9,, ,,10,10,, 

the script works calculating max number of lines in files , appending rows other files. example, in case, file2 has 7 rows , file1 has 4, script temporarily add 3 comma-delimited rows file1. finally, paste used merge files.


Comments