[Solved] text manipulation by addition and multiplication


Here’s another version if you can’t work with the other answer:

awk -vval=2.1 '{              # set "val" to the new value for column 3 on first two lines
  if(NR==1 || NR==2) {        # if it's the first or second line
    $3=val;                   # set column 3 to val
    $2=$3*2.9                 # set column 2 to column 3 multiplied with 2.9
  } else if(NR>=3 && NR<=5) { # else if it's line 3-5
    $3=val+0.2;               # set column 3 to val+0.2
    $2=$3*4.227               # set column 2 to column 3 multiplied with 4.227
  } else $3=$3;               # just for formatting
  print                       # print the result
}' test_file

Remove the comments (#) before you run it.

Output:

0.00 6.09 2.1 5.0 6.0 8.0 0.0
10.00 6.09 2.1 1.0 1.0 1.2 9.6
10.00 9.7221 2.3 2.0 1.4 2.5 9.6
30.00 9.7221 2.3 1.2 1.5 1.9 1.4
30.00 9.7221 2.3 3.2 2.4 1.2 4.1
60.00 9.8 3.5 1.4 2.7 3.2 4.5

To loop over a range and save it in different files you can do like below. I also made the other parameters available so you can set them when running the script:

#!/bin/bash

for val in $(seq 2.1 0.1 2.5)
do
  awk -vval=$val -vfmul=2.9 -vadd=0.2 -vsmul=4.227 '{
    if(NR==1 || NR==2) {
      $3=val;
      $2=$3*fmul
    } else if(NR>=3 && NR<=5) {
      $3=val+add;
      $2=$3*smul
    } else $3=$3;
    print
  }' test_file > output$val
done

1

solved text manipulation by addition and multiplication