Even below one can give desired output, if your real input file is same as what you have posted.
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 string "R,S,T"
by separator comma, and save in array a
, so it becomes a[1] = R, a[2] = S, a[3] = T
f=$2~/^H[0-9]+$/
- f
is variable, validate regexp $2 ~ /^H[0-9]+$/
, which returns boolean status. if it returned true
then variable f
will be true, otherwise false
$2 = $2 a[++c]
if above one was true, then modify second field, so second field will have existing value plus array a
value, corresponding to the index (c
), ++c
is pre-increment variable
!f{c=0}
if variable f
is false then reset variable c
, not consecutive.
1
at the end does default operation that is print current/record/row, print $0
. To know how awk works try, awk '1' infile
, which will print all records/lines, whereas awk '0' infile
prints nothing. Any number other than zero is true, which triggers 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 better formatting like tab
or some other char as field separator, then you may use below one, modify 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