Gloom
Gloom

Reputation: 317

Adding a counter to a specific string using unix

I am trying to add a counter to a specific string using unix, I have tried some sed and awk commands but I can't seem to do it properly.

My input file is:

Event_ A     D       L       K
Event_ B     P       R
Event_ C     F       I
Event_ J     K
M
N
O
Event_ Q     S
X
Y
Z
G
T

What I'm hoping to get is:

Event_00000001    A     D       L       K
Event_00000002    B     P       R
Event_00000003    C     F       I
Event_00000004    J     K
M
N
O
Event_00000005    Q     S
X
Y
Z
G
T

Can anyone help?

Upvotes: 0

Views: 60

Answers (1)

sat
sat

Reputation: 14949

Use this awk:

awk '/^Event/{$1=sprintf("%s%06d", $1,++counter)}1' yourfile

If fields are delimited by \t(Tab),

awk -F"\t" '/^Event/{$1=sprintf("%s%06d", $1,++counter)}1' OFS='\t' yourfile

Test:

$ awk '/^Event/{$1=sprintf("%s%06d", $1,++counter)}1' file
Event_000001 A D L K
Event_000002 B P R
Event_000003 C F I
Event_000004 J K
M
N
O
Event_000005 Q S
X
Y
Z
G
T

Upvotes: 3

Related Questions