benutzer_a_b
benutzer_a_b

Reputation: 3

Search and replace following numbers

In a file containing number ranges (4-5, 12-20), I want to replace all "number-number+1" ranges by "number f." (45-46 > 45 f.).

Any idea how to achieve that? Sorry if this has been covered before, I found nothing until now.

Upvotes: 0

Views: 76

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172768

As this condition (M + 1 = N) is hard to express in a regular expression, this is a case for a :help sub-replace-expression: Match all number ranges, separating them into start and end numbers via \(...\) capture groups. Then in the \= replacement expression, check for the condition, accessing the numbers via submatch(), and either return the original result (submatch(0)), or the condensed form.

:%substitute/\(\d\+\)-\(\d\+\)/\=submatch(1) + 1 == submatch(2) ? submatch(1) . ' f.' : submatch(0)/g

Upvotes: 3

ZOMGnerd
ZOMGnerd

Reputation: 21

If the file is already created use the GREP command to parse the file and SED command to replace. Or use find. Both ways I'm sure are on here.

Upvotes: 0

Related Questions