Replace values in script reading line by line using sed

Hi all,

Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE.

for pa in PA_VALUE 
blah blah
do
for pb in PB_VALUE
blah blah
do

I have a text file with two columns of values for PA and PB.

14.5 16.7
7.8 9.5
5.6 3.6
etc etc

I would like to read this text file line by line and replace the PA_VALUE and PB_VALUE with these values. The first row, first column would replace wherever PA_VALUE occurs in the script and replace the value in the first row, second column wherever PB_VALUE occurs. Then it would repeat line by line in the text document. I have thus been unable to achieve this. I am familiar with sed to replace values, but unsure how to link it to read a document line by line and replace in another script. Thanks.

Hello crimsonengineer,

Welcome to forum. Please let us know input and expected output for same.
It will be helpful for us.

Thanks,
R. Singh

So my output would be a script containing the values in the text file:

I would like my script to repeat for each row in the text file. I think this can be accomplished with sed or awk, but unsure how to piece together the proper commands. Thanks so much.

Hello,

Following may help in same.

awk '{for(i=1;i<=NF;i++){{if(i%2 != 0){print "for pa in " $i ORS "do" ORS "blah blah" ORS "done\n"} else {print "for pb in " $i ORS "do" ORS "blah blah" ORS "done\n"}}}}' Input_file

Output will be as follows.

for pa in 14.5
do
blah blah
done
 
for pb in 16.7
do
blah blah
done
 
for pa in 7.8
do
blah blah
done
 
for pb in 9.5
do
blah blah
done
 
for pa in 5.6
do
blah blah
done
 
for pb in 3.6
do
blah blah
done
 
for pa in etc
do
blah blah
done
 
for pb in etc
do
blah blah
done

EDIT: Adding more solutions for same.

awk '{for(i=1;i<=NF;i++){V=i%2 != 0?(V="pa "):(V="pb "); {print "for " V "in " $i ORS "do" ORS "blah blah" ORS "done\n"}}}'  Input_file
OR
awk '{for(i=1;i<=NF;i++){{if(i%2 != 0){V="pa "} else {V="pb "}; print "for " V "in " $i ORS "do" ORS "blah blah" ORS "done\n"}}}' Input_file

Thanks,
R. Singh

In shell:

while read pa pb
do
  cat << EOF
for pa in $pa 
blah blah
do
for pb in $pb
blah blah
do
EOF
done < file

--
Note that

for pa in 14.5
blah blah
do

Is invalid syntax but that wasn't your point..

1 Like

Thanks for the replies. I think this last bit might work. But if I had other conditions in my script ... could I embed it like this:

for var1 in 1 2
do
for var2 in 3 4
do

while read pa pb
do
  cat << EOF
for pa in $pa 
do
for pb in $pb
do
EOF
done < file

[sed commands to replace the above values in another file]

done #end var1
done #end var2

so would the embedded while/do loop also iterate through the lines in the "file" along with the previous for/do loops?

You can use the "here document" ( cat << EOF etc ) to generate text but not to somehow dynamically generate code that is part of the script itself. So as it is it will print the text to stdout..

But it is unclear what you intend to do. Can you elaborate (for example with the sed command that you mention)?

Thanks, so backing up.

I have a text file with values of pa and pb like this:

1.3 5.6
1.3 5.8
1.3 6.0
etc etc

I have a script that currently does the following:

## loop through possible values
for var1 in 1 2
do
for var2 in 2 3
do
for pa in 5 6 7
do
for pb in 7 8 9
do

sed -e "s/.*var1.*/$var1 var1/g" -e "s/.*var2.*/$var2 var2/g" -e "s/.*pa.*/$pa pa/g" -e "s/.*pb.*/$pb pb/g" param.def > param.tmp

So the sed command says: wherever you see "var1" in param.def, replace with $var1 and text var1. So the row looks like "$var1 var1". Similar to var2, pa, and pb.

The difference is that I have produced a variety of pa and pb values already in a separate text file. I would somehow like to introduce a method to read these line by line as $pa and $pb, and still be replaced in this other file via sed. Does that make sense?