a Sed question

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Fri Feb 2 10:41:08 AEST 1990


In article <520 at mirsa.UUCP> muller at mirsa.UUCP (Christophe Muller) writes:
: I wanted to find all the lines matching a
: pattern, delete these lines _and_ the line immediately following.
: 
: After half an hour of tests, man, Unix books, i gave up and used a
: small awk script that worked well the first time ! I just thought that
: sed might be faster, but is it possible ?

Certainly.

/pattern/{
    N
    d
}

If pattern matches, append the next line to the current line, and then
discard the whole pattern buffer.

: (Note: we still don't have perl...)

It doesn't buy you much in this case, depending on the pattern.  Searching
for /window/ in /etc/termcap, perl beats sed by only .2 seconds, less than
a 20% speedup.  On a more complicated pattern, you might get more.  Or less.

That's using the perl command:

    time perl -pe '<>,$_ = "" if /window/;' /etc/termcap > /dev/null
    1.0u 0.1s 0:02 40% 191+162k 23+5io 7pf+0w
vs
    time sed -f window.sed /etc/termcap >/dev/null
    1.2u 0.1s 0:02 56% 29+40k 24+0io 1pf+0w

where window.sed contains the appropriate commands.

awk weighs in at

    time awk '/window/{getline;next}{print}' /etc/termcap > /dev/null
    2.3u 0.1s 0:03 72% 55+112k 22+0io 4pf+0w

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.questions mailing list