matching and replacing across line breaks

Francois-Michel Lang lang at PRC.Unisys.COM
Fri Dec 15 01:41:00 AEST 1989


Can anyone provide a solution to this problem,
preferably using sed or awk or some such utility:

I want to operate on a file in such a way that every occurrence of
"foo" at the end of one line followed by "bar" at the beginning of the
next line is replaced by "quux".  I have a way to do this using sed
that works *most* of the time, but there are certain cases in which
it fails.

Specifically, on input

---
a
s
d
xxxfoo
barfoo
baryyy
q
w
e
foo
bar
t
y
last foo line
---

I want to produce

---
a
s
d
xxxquuxquuxyyy
q
w
e
quux
t
y
last foo line
---

The sed script I have (see below) won't handle
multiple consecutive occurrences of "foo\nbar",
and it also has a nasty habit of discarding the very last
input line if it happens to contain a "foo".

/foo/   {
                N
                s/foo\nbar/quux/
        }
----------------------------------------------------------------------------
Francois-Michel Lang
Paoli Research Center, Unisys         lang at prc.unisys.com      (215) 648-7256
Dept of Comp & Info Science, U of PA  lang at linc.cis.upenn.edu  (215) 898-9511



More information about the Comp.unix.questions mailing list