Wednesday, September 27, 2023

Re: Replace spaces with underscore after some pattern in line

On Mi, 27 Sep 2023, Igor wrote:

> How to replace all of the spaces in each line after XXX string?
>
> Input sample data:
> aaaaaa bbbbbb cccc XXX aaaaa bbbbbbb
> cccccccc eeeeeeeeee XXX aaa bbbb cccc ddd
> aaaa bbbb ddd ee XXX aaa bbb dddd eee
>
> Output sample data:
> aaaaaa bbbbbb cccc XXX_aaaaa_bbbbbbb
> cccccccc eeeeeeeeee XXX_aaa_bbbb_cccc_ddd
> aaaa bbbb ddd ee XXX aaa_bbb_dddd_eee

That is a bit tricky. I would probably use something like this:

:%s/\(XXX.*\)$/\=substitute(submatch(1), ' ', '_', 'g')/

Which basically grabs everything after the 'XXX' and replaces in that
submatch all whitespaces using a sub-replace-expression (see :h
sub-replace-expression and :h sub-replace-special).

Thanks,
Christian
--
Don't go surfing in South Dakota for a while.

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/ZRQdNCkqwetPL1%2BV%40256bit.org.

No comments: