First of all: I hope the question is comprehensible, otherwise please let me know and I will do my best to make it more clear.This is a follow up to Replace one entire column in one file with a single value from another file combined with R Pass Variable from R to UnixHere goes:I am running several scripts (Perl, python and R) in one Unix script and need to pass outputs of these scripts to Unix and combine information from different files these scripts create.I have a working code which is a combination of the above mentioned questions:The output from the R script called getenergie.R is a filename. There are several such filenames which are returned and I need to write into each of these files and replace column 11 of these files with a value which comes from another file (COP1A_report1) and is called value. RES=($(./../getenergies.R))for pdbnames in ${RES[@]}do#write the one value from COP1A_report1 into column 11 of a file and save as TESTINGvalue=$(awk -F, 'NR==1{print $2;exit}' ./../COP1A_report1) awk '{$11 = v} 1' v=$value ${pdbnames} > TESTINGprintf $value ${pdbnames}\ndoneWhat I need is a way to loop over this so that it writes one value from COP1A_report1 (row $2, line 1)  into column 11 of a file called like the first filename stored in $pdbnames, save it as a unique file and go to COP1A_report1 (row $2, line 2), write that into column 11 of a file called like the second filename stored in $pdbnames and so on...What is the smartest way of doing that? I can imagine sth like the code below, but sth is wrong with the syntax and I do not get any errors, just an empty value variable.Any ideas?counter=1RES=($(./../getenergies.R))for pdbnames in ${RES[@]}dovalue=$(awk -F, 'NR==$counter{print $2;exit}' ./../COP1A_report1) #NR=1 needs to be changed to go through the entire list...awk '{$11 = v} 1' v=$value ${pdbnames} > TESTING$countercounter=$(echo $counter+1 |bc)printf $value ${pdbnames}\ndone
combining output from different scripts into different files in a loop
files;output;loop device
You're trying to pass $counter into the awk script, so you need to use double-quotes rather than single-quotes.  Single-quotes are for literal strings, double-quotes are for strings with variables in them.Because you're using double-quotes here, that means you have to escape $2 as \$2 in the awk script so that the shell doesn't substitute it's second arg (if any, empty string otherwise) into the awk script.value=$(awk -F, NR==$counter{print \$2;exit} ./../COP1A_report1You should also quote other variables when you use them.  e.g. awk ... > TESTING$counter - lack of quotes was harmless in this instance, but always quoting your variables is a good habit to get into.Same for counter=$(echo $counter+1 |bc) - not quoting is harmless here, but still bad practice.Finally, you're missing the -v from the awk command when you set v=$value.  should be -v v=$valuecounter=1RES=($(./../getenergies.R))for pdbnames in ${RES[@]}do    value=$(awk -F, NR==$counter{print \$2;exit} ./../COP1A_report1)    awk -v v=$value '{$11 = v} 1' $pdbnames > TESTING$counter    counter=$(echo $counter+1 |bc)    printf $value $pdbnames\ndone