Shell – Loop over a file and read values from two columns into variables

shell-scripttext processing

I have a file (data.dat) which contains 3 columns. I want to read in two of them with the shell script and fill the contents into a template file.
The file looks like this:

12 180 2390
14 177 2210
16 173 2130
...

So far I can do that with one variable (which is column 2 in the data-file) as follows:

for a in `cat data.dat | gawk '{print $2}'`; do
 sed s/A/$a/ daily.template daily.template > daily.inp
done

Now I want to read in column 3 as well and give the according value (b) of the same line as column 2 to the same template. The template contains variables called "A" and "B".
I cannot do a nested for loop as I don't want to iterate each value of a over each value of b.

How can I achieve this?

Addition:

First of all thanks for all your help!

Yes, it might be more easy to understand if I give more information about the daily.template file. It is looking similiar to:

dens_column O3 300.00
dens_column h20 B
sza A
day_of_year 172
output sum
...

A and B should be replaced in every loop over the lines.
The first column in data.dat does not mean anything, it just displays the time of a measurement and this parameter is not needed in "daily.inp".
I just want one "daily.inp" for each line where A and B are replaced with the according value of column 2 and 3 in the according line.
So a daily.inp file like:

dens_column O3 300.00
dens_column h20 2390
sza 180
day_of_year 172
output sum
...

Then the next file separate:

dens_column O3 300.00
dens_column h20 2210
sza 177
day_of_year 172
output sum
...

And so on…

Best Answer

Then that should only be a matter of:

while read discard x y discard; do
  sed "s/A/$x/g;s/B/$y/g" < daily.template > "daily.$x-$y.inp"
done < data.dat

The read command will read one line at a time from data.dat, put the first field (fields being blank separated, and where backslash can be used to escape the field or line separator) in the variable $discard, the second in $x, the third in $y and the rest of the fields in $discard.