Reputation: 880
I'd like to use the index of a for loop in bash to calculate a number then pass that number to sed:
for i in {1989..2009..1}
do
let year=i-1
echo $year
find /cygdrive/d/snowmodel/$i/snowmodel.par -type f -exec \
sed -i 's/iyear_init = 1989/iyear_init = '$year'/g' {} +
done
So I want to replace the line iyear_init = 1989
with the line iyear_init = (the value of i-1, which should be the variable "year")
.
the echo $year
command returns the correct value, but it seems that when it gets passed to sed it reverts back to treating year like a string.
Thanks for any help.
Upvotes: 1
Views: 219
Reputation: 880
Ok found answer after stackoverflow suggested Related questions (I guess my search queries weren't worded very well).
The problem was I needed to replace the single quotes with double quotes because variables inside single quotes don't get replaced in sed. The following worked:
for i in {1989..2009..1}
do
let year=i-1
echo $year
find /cygdrive/d/snowmodel_cig/cig_79/bias_runs/$i/snowmodel.par -type f -exec \
sed -i "s/iyear_init = i-1/iyear_init = $year/g" {} +
done
Upvotes: 1