Even below you can give the desired result if your real input file matches what you published.
awk 'BEGIN{split("R,S,T",a,/,/)}f=$2~/^H[0-9]+$/{$2 = $2 a[++c]}!f{c=0}1' infile
Explanation
split("R,S,T",a,/,/) - split the string "R,S,T" into a separator comma and save it in array a , so it will become a[1] = R, a[2] = S, a[3] = T
f=$2~/^H[0-9]+$/ - f is a variable, validate regexp $2 ~ /^H[0-9]+$/ , which returns a logical state. if it returns true , then the variable f will be true, otherwise false
$2 = $2 a[++c] , if the one above was true, then change the second field, so the second field will have the existing value plus the value of the array a corresponding to index ( c ), ++c increment variable
!f{c=0} if the variable f is false, then reset variable c rather than sequential.
1 At the end, the default operation is performed, which is the current print / record / line, print $0 . To find out how awk works, awk '1' infile , which will print all records / lines, and awk '0' infile does not print anything. Any number other than zero is true, which causes the default behavior.
Test results:
$ cat infile 1ECLI H813 98 7.529 8.326 9.267 1ECLI H813 99 7.427 8.470 9.251 1ECLI C814 100 7.621 8.513 9.263 1ECLI H814 101 7.607 8.617 9.289 1ECLI H814 102 7.633 8.489 9.156 1ECLI H814 103 7.721 8.509 9.305 1ECLI C74 104 8.164 8.733 10.740 1ECLI H74R 105 8.247 8.690 10.799 $ awk 'BEGIN{split("R,S,T",a,/,/)}f=$2~/^H[0-9]+$/{$2 = $2 a[++c]}!f{c=0}1' infile 1ECLI H813R 98 7.529 8.326 9.267 1ECLI H813S 99 7.427 8.470 9.251 1ECLI C814 100 7.621 8.513 9.263 1ECLI H814R 101 7.607 8.617 9.289 1ECLI H814S 102 7.633 8.489 9.156 1ECLI H814T 103 7.721 8.509 9.305 1ECLI C74 104 8.164 8.733 10.740 1ECLI H74R 105 8.247 8.690 10.799
If you want to format better, for example, tab or some other char as a field separator, then you can use below one, change the OFS variable
$ awk -v OFS="\t" 'BEGIN{split("R,S,T",a,/,/)}f=$2~/^H[0-9]+$/{$2 = $2 a[++c]}!f{c=0}{$1=$1}1' infile 1ECLI H813R 98 7.529 8.326 9.267 1ECLI H813S 99 7.427 8.470 9.251 1ECLI C814 100 7.621 8.513 9.263 1ECLI H814R 101 7.607 8.617 9.289 1ECLI H814S 102 7.633 8.489 9.156 1ECLI H814T 103 7.721 8.509 9.305 1ECLI C74 104 8.164 8.733 10.740 1ECLI H74R 105 8.247 8.690 10.799
source share