I want to give a value from a file read to a variable, then use it to modify all elements of a column in another file, specifically to shift the entries by some number.
The data file "file_to_change" has two columns of numerical data, one to be shifted in value. Like so:
-1.643465E+01 1.378835E-03
-1.642464E+01 1.242392E-03
-1.641463E+01 1.147501E-03
-1.640462E+01 1.083067E-03
-1.639461E+01 1.042069E-03
-1.638460E+01 1.020041E-03
-1.637459E+01 1.014211E-03
The file "tmp" which contains the shifting value is simply this:
-11.43465
I current have the following code:
file=tmp
var1=$(awk 'NR==1' $file)
echo $var1
awk '{print ($1 - $var1) " " $2}' file_to_change
echo $var1
My result is:
-11.43465
0 1.378835E-03
0 1.242392E-03
0 1.147501E-03
0 1.083067E-03
0 1.042069E-03
0 1.020041E-03
0 1.014211E-03
-11.43465
I imagine this is pretty straightforward, but I can't see where the error is!
$var1). You need to give it's value to awk variable like:awk -v var1="$var1" '{ ... var1 ...awk -v 'var1=value' '{print ($1 - $var1) " " $2}' file_to_change(In awk you refer to the variable as var1 and not $var1)