How to use paste command recursively in a shell script
I would suggest doing the whole thing in awk, rather than using a shell loop:
awk '{for (i=1; i<=12; ++i) printf "%d%s", $1+i, (i<12?FS:RS)}' "$f1" > output
This loop runs for each line of the file and outputs all of the columns, saving the need to use another tool such as paste
. The ternary operator is used to output a space (FS
) between each column and a newline (RS
) at the end of the line.
For the second part of your question, you can still do most of the work in awk.
declare -A companiescompanies=( [company1]=10 [company2]=25 [company3]=50 )for c in "${companies[@]}"; do awk -v bonus="${companies[c]}" '{for (i=1; i<=12; ++i) printf "%d%s", $i+(i==1||i==5||i==9?bonus:5), (i<12?FS:RS)}' "$f1" > "$c.txt"done
I have created an associative array containing each company name and their bonus. The loop goes through each company in the array, passing the bonus to awk as a variable (note that I'm doing this using -v
rather than using string concatenation). The loop goes from 1 to 12, adding the bonus for months 1, 5 and 9, or 5 otherwise. The name of the company (the key of the shell array) is used for the output file.
I can't understand, why are you going in such complicated way. You can do it in a more simpler way:
for 2nd one:
for k in var{[@]}do awk '{printf("%.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f\n", $1+"$bonus_$k", $1+5, $1+5, $1+5, $1+"$bonus_$k", $1+5, $1+5, $1+5, $1+"$bonus_$k", $1+5, $1+5, $1+5);}' file.txt > $k_file.txtdone