0

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.