Bash 向数组中的每列添加不同的值

Bash 向数组中的每列添加不同的值,bash,awk,Bash,Awk,如何向bash脚本中的每个列添加不同的值 示例:在x上绘制的三个函数f1(x)f2(x)f3(x) test.dat: # x f1 f2 f3 1 0.1 0.01 0.001 2 0.2 0.02 0.002 3 0.3 0.03 0.003 现在我想给每个函数添加一个不同的偏移值 values = 1 2 3 预期结果: # x f1 f2 f3 1 1.1 2.01 3.001 2 1.2 2.02 3

如何向bash脚本中的每个列添加不同的值

示例:在x上绘制的三个函数f1(x)f2(x)f3(x)

test.dat:

# x  f1    f2    f3 
1    0.1  0.01  0.001
2    0.2  0.02  0.002
3    0.3  0.03  0.003
现在我想给每个函数添加一个不同的偏移值

values = 1 2 3
预期结果:

# x  f1    f2    f3 
1    1.1  2.01  3.001
2    1.2  2.02  3.002
3    1.3  2.03  3.003
所以第一列应该不受影响,否则增加的值

我试过了,但没用

declare -a energy_array=( 1 2 3 )

for (( i =0 ; i <  ${#energy_array[@]} ; i ++ ))
do
local energy=${energy_array[${i}]}
cat "test.dat" \
 | awk -v "offset=${energy}" \ 
'{ for(j=2; j<NF;j++) printf "%s",$j+offset OFS; if (NF) printf "%s",$NF; printf ORS} '
done 
declare-a能量数组=(1 2 3)
对于((i=0;i<${#energy_数组[@]};i++)
做
局部能量=${energy_数组[${i}]}
cat“测试数据”\
|awk-v“offset=${energy}”

{对于(j=2;j您可以尝试以下方法:

declare -a energy_array=( 1 2 3 )
awk -voffset="${energy_array[*]}" \
'BEGIN { n=split(offset,a) } 
 NR> 1{ 
   for(j=2; j<=NF;j++) 
      $j=$j+a[j-1]
   print;next
 }1' test.dat

您可以尝试以下操作:

declare -a energy_array=( 1 2 3 )
awk -voffset="${energy_array[*]}" \
'BEGIN { n=split(offset,a) } 
 NR> 1{ 
   for(j=2; j<=NF;j++) 
      $j=$j+a[j-1]
   print;next
 }1' test.dat

很有魅力。谢谢!很有魅力。谢谢!