Razvan
Razvan

Reputation: 140

Increment (1st) number of each line found in file with one

I need to increment the numbers form a file using awk/sed/... .

file:

step0=action_a
step1=action_b
step2=action_c

output:

step1=action_a
step2=action_b
step3=action_c

I've tried with:

awk '/step/{ $2=$2+1 }' file

but the step numbers are not incremented.

Upvotes: 3

Views: 455

Answers (2)

Kent
Kent

Reputation: 195229

awk can do it for sure. however, with vim, your problem could be solved very easily.

vim -c "%norm! ^A" file

the ^A you press Ctrl-V Ctrl-A

then check the changed text in your vim, if you are satisfied, then press :wq save the file and quit.

If this is not the answer you are looking for, let me know, I would remove the answer.

Upvotes: 1

user000001
user000001

Reputation: 33387

Using Ed Morton's answer on a similar question, you could do it like this:

$ awk -F'[^[:digit:]]+' '{sub(/[[:digit:]]+/,$2+1)}1' file
step1=action_a
step2=action_b
step3=action_c

This replaces the first number (i.e. sequence of digits) with the matched number plus one.

Upvotes: 2

Related Questions