Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!umich!samsung!cs.utexas.edu!asuvax!mcdphx!mcdchg!ddsw1!dattier From: dattier@ddsw1.MCS.COM (David W. Tamkin) Newsgroups: comp.editors Subject: Re: ex search & substitution question Message-ID: <1990Aug08.200822.13213@ddsw1.MCS.COM> Date: 8 Aug 90 20:08:22 GMT References: <3440001@hprnd.HP.COM> <26814@nigel.ee.udel.edu> <1990Aug07.174235.28911@hoss.unl.edu> Reply-To: dattier@ddsw1.MCS.COM (David W. Tamkin) Organization: Contributor Account at ddsw1, Wheeling, Illinois Lines: 39 Sanjiv K. Bhatia wrote in <1990Aug07.174235.28911@hoss.unl.edu>, answering Darren New's response to Brent McInnis: DN> Try DN> :10,20g/^#[iut]55/s/55/71/ DN> I think you can give a range to the g command to have it operate on the DN> lines within the range that also match the pattern. -- Darren Yes, you can give a firstline,lastline range and then a g or v within it, but you cannot nest a g or a v within a[nother] g or v. SKB> Here is my $0.02 worth. I prefer to use the substitute command 's'. SKB> :10,20s/^#[iut]55/77/ Not quite, Sanjiv. That will lose the # and the letter, replacing all four characters with just "77". Darren's suggestion of :10,20 g/^#[iut]55/s/55/71/ will work *for this situation*. It says to look for #[iut]55 at the beginning of any of those eleven lines in the numeric range and change the leftmost 55 to 71 in every line containing that pattern. It depends, however, on the expression's being anchored to the left end of the line, or there would be a risk of changing the wrong 55 to 71 if 55 occurs again farther leftward in the line. It also depends on the first part of the search expression's not containing the last part: if Brent had wanted to change /^5555/ to 5571, he'd get 7155! :10,20 s/^\(#[itu]\)55/\171/ gets around that problem; it says to look for the four-character pattern and change the 55 in its last two characters to 71, leaving the first two characters unaltered, regardless of what they were. Its reasoning can be applied to search patterns that are not left-edge anchored. I like the logic behind this method better, but in this particular case the risks in Darren's advice are not present, and I find his way less subject than my own to the particular typing errors I'm prone to make. (Neither method requires a space after the 20; it's just another personal preference of mine, as was alphabetizing [itu].) David Tamkin Box 7002 Des Plaines IL 60018-7002 708 518 6769 312 693 0591 MCI Mail: 426-1818 GEnie: D.W.TAMKIN CIS: 73720,1570 dattier@ddsw1.mcs.com