Re: lines matching N and N+1

Davide Brini <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On Fri, 01 Feb 2013 02:10:42 -0000, "Daniel" <[email protected]> wrote:

> Yes, I did mean "word" as in GNU sed \<word\> sense.
> 
> With the "test file" and "desired output" I mentioned, 
> here's what I came up with, similar to something I saw
> from Y-J Chang. It assumes using -r command line option.
> 
> $! N 
> s:(.*)\<foo\>(.*)\n(.*)\<bar\>(.*):\1FOO\2\n\3BAR\4:

Note that you don't need to capture group \1 and \4, for the same reason
that to replace foo with bar you do

s/foo/bar/

and not

s/(.*)foo(.*)/\1bar\2/

(leaving aside that the latter form can behave differently under certain
circumstances).

> t    

There's no harm in printing the first line of the pattern space, so the "t"
can be removed. In fact, every time "t" is executed, you're effectively
adding two more lines to the pattern space without removing the existing
ones. This can lead to false matches in the s::: replacement (ie, times
where foo and bar are more than one line apart, yet the replacement
succeeds).

> P; D 
> 
> # Append next line if NOT last line of file
> # Try changing foo in line #1, bar in line #2
> # If change happened, print lines and read a new pair
> # Else, print & delete line #1, run N to make next pair
> 
> This seems similar to some of the other solutions 
> that were posted.
> 
> Thanks,
> Daniel

-- 
D.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.