3

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.

Razvan
  • 140
  • 9

2 Answers2

2

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.

Community
  • 1
  • 1
user000001
  • 32,226
  • 12
  • 81
  • 108
1

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.

Kent
  • 189,393
  • 32
  • 233
  • 301