I have a number of variables in a .dat file that I automatically changed with a script. One of these files has a parameter, bar
, that is arranged as such in a .dat file:
var1 var2 var3 foo bar
T T T 100 100
I used to use the following lines of a bash script to change the value of bar from an arbitrary initial value to the desired value, in this case 2000. This script would change 'bar' to 2000.
LINE1=$(awk '/bar/ {++n;if (n==1) {print FNR}}' data.dat)
((LINE1=$LINE1 + 1))
OLD1=$(awk '{for(i=1;i<'$LINE1';i++) getline}{print $12}' data.dat)
sed -i '' "${LINE1}s/$OLD1/2000/" data.dat
However, I now must now change foo
alongside bar
. In this example, this is setting foo
and bar
both to 2000.
LINE1=$(awk '/foo/ {++n;if (n==1) {print FNR}}' data.dat)
((LINE1=$LINE1 + 1))
OLD1=$(awk '{for(i=1;i<'$LINE1';i++) getline}{print $12}' data.dat)
sed -i '' "${LINE1}s/$OLD1/2000/" data.dat
LINE1=$(awk '/bar/ {++n;if (n==1) {print FNR}}' data.dat)
((LINE1=$LINE1 + 1))
OLD1=$(awk '{for(i=1;i<'$LINE1';i++) getline}{print $12}' data.dat)
sed -i '' "${LINE1}s/$OLD1/2000/" data.dat
This instead only changed the foo
to 2000 while leaving bar
unchanged. I realize that this is an issue with the way I've described the regular expression, but I have been unable to change both variables with an awk/sed expression.
fort.4
so people can follow your logics. Do you want to change thebar
text or the number100
?foo
andbar
rather than the stringsfoo
andbar