Script to rename the repeating strings

All,
I have a sample text like below.

Key (Header)
Key1
ABC
Key2
ABC
Key3
ABC
ABC
Key4
ABC
Key5
ABC
ABC
ABC

Required Output

Key (Header)
Key1
ABC_1
Key2
ABC_1
Key3
ABC_1
ABC_2
Key4
ABC_1
Key5
ABC_1
ABC_2
ABC_3

Here my requirement is to encode the string ABC coming after every key.
If it occurs only once then rename as ABC_1. Else should be renamed as ABC_1, ABC_2 etc for each subsequent entry.

Thanks in advance.
Sidda

awk '!/ABC/{c=0}/ABC/{$0=$0"_"++c}1' file
1 Like

Your script worked very well.
Thank you so much !!

Sidda