Wednesday, September 27, 2023

Re: Replace spaces with underscore after some pattern in line





------ Original Message ------
From: "c.willis111" <c.willis111@btinternet.com>
To: vim_use@googlegroups.com
Sent: Wednesday, 27 Sep, 2023 At 12:49
Subject: Re: Replace spaces with underscore after some pattern in line




------ Original Message ------
From: "Igor" <igor2x@gmail.com>
To: "vim_use" <vim_use@googlegroups.com>
Sent: Wednesday, 27 Sep, 2023 At 12:09
Subject: Replace spaces with underscore after some pattern in line

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




--
Hi

to my slight surprise this works:


:%s/\(XXX[^ ]*\) /\1_/g


The reason I had doubts is that the successive replacements start at the beginning of the earlier replacement.


(Sorry about layout. I'm not into this add at the end bit).


regards - Chris


Oh no it doesn't. One needs to repeat until all done. The g is pointless. - Chris

No comments: