Rutherford Hayes
Rutherford Hayes

Reputation: 63

VIM: match number following PATTERN, replace with number + 1

I have a set of lines that look like:

 $AERDIR/aergen.sh -n control -s 2100 -e 2120 -m 3 -a -x -z 144 -p  -g
 $AERDIR/aergen.sh -n Cl26E10 -s 2100 -e 2120 -m 3 -a -x -c 2.6E-10 -z 145 -p  -g
 $AERDIR/aergen.sh -n Br26E12 -s 2100 -e 2120 -m 3 -a -x -b 2.6E-12 -z 146 -p  -g
 $AERDIR/aergen.sh -n I26E13  -s 2100 -e 2120 -m 3 -a -x -i 2.6E-13 -z 147 -p  -g

I would like to match the three digit number after -z and replace it with that number +1, so

 $AERDIR/aergen.sh -n control -s 2100 -e 2120 -m 3 -a -x -z 145 -p  -g
 $AERDIR/aergen.sh -n Cl26E10 -s 2100 -e 2120 -m 3 -a -x -c 2.6E-10 -z 146 -p  -g
 $AERDIR/aergen.sh -n Br26E12 -s 2100 -e 2120 -m 3 -a -x -b 2.6E-12 -z 147 -p  -g
 $AERDIR/aergen.sh -n I26E13  -s 2100 -e 2120 -m 3 -a -x -i 2.6E-13 -z 148 -p  -g

I've been playing around with submatch parameters and such, but have only managed to increment the first appearance of numbers in each line. How would I formulate a command to do this, say from line 203,$ ?

Upvotes: 1

Views: 723

Answers (1)

builder-7000
builder-7000

Reputation: 7627

This is a job for submatch:

%s/-z \zs\d\+/\=submatch(0)+1/

The pattern -z \zs\d\+ matches one or more digits \d\+ preceeded by -z. Then \=submatch(0)+1 adds one to the matched number.

Upvotes: 4

Related Questions