Thursday, February 15, 2018

Re: Change only second match on a line...

On 02/15/2018 06:54 PM, Tim Chase wrote:
> The easiest way on most platforms is to just pipe it through sed which
> does the dirty work for you:
>
> :%! sed 's/old/new/2'

Yep, that would do it.

Sometimes the solution is to look outside of vim.

> If you don't have sed available...

I shudder to think of such systems.

I have grown EXTREMELY fond of the standard unix text processing
environment.

I was creating histograms with awk earlier today. (Something like 20 of
them.)

> ...this would be the route you'd go.
>
> :%s/old.\{-}\zsold/new
>
> for just two, or more generically for an arbitrary Nth place (e.g. N=4)
>
> %s/\%(old\%(\%(old\)\@!.\)*\)\{3}\zsold/new
>
> where the "3" is N-1. Indeed ugly.

Ew. I think I'll step over that pile of annoying for now and stick with
your good sed recommendation.

> Thus my recommendation for the simplicity of sed over the pure-vim
> solution.

Indeed.

Thank you again Tim.



--
Grant. . . .
unix || die

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment