I have the following requirement to add a sequence value to each record in a file but only if it meets certain conditions. Field value in pos. 1 and 2 must be '0B' or 'OA' else leave as is. Sequence value must be preserved for each OB and OA pair.
This is what I have so far but it's not working 100% as expected. It's adding the sequence value to the last record also.
awk '{var0=substr($0,1,2)} var0=="AB" {COUNTER = COUNTER+1} {$0 = $0 FS COUNTER} {var1=substr($0,1,2)} var1=="AO" {$0 = $0 FS COUNTER} 1'