Article View: comp.editors
Article #4042SED q. re multiline patterns (long)
From: Eric Pement
Date: Sun, 22 Oct 1995 00:00
Date: Sun, 22 Oct 1995 00:00
77 lines
3931 bytes
3931 bytes
An advanced SED question involving multi-line patterns (I'm using GNU sed
v1.5 by Howard Helman, compiled for MS-DOS, but I think this applies to
several varieties of SED):
I want to remove a block of 4 contiguous lines which recur at repeated
intervals throughout several files. The first line of each block is
always a blank line. However, the input files also contain groups of 1
or more blank lines which should not be deleted. My problem arises when
the block to be deleted is preceded by 1 or more blank lines. E.g.,
1: aaa aaaa # (Line numbers added for clarity)
2: bbb bbbb
3:
4: # Lines #4-7, inclusive,
5: xxxx # should be deleted from
6: yyyy # the input stream.
7: zzzz #
8: ccc cccc
9: ddd dddd
I have tried writing many SED scripts to delete this pattern, with
varying degrees of success. My best effort is this one:
/^$/ { # locate a blank line
N # .. append the next line to pattern space
/xxxx/ D # .. if "xxxx" is in the space, delete blank line
} # only the second line will be output.
/xxxx/ { # locate lines with "xxxx"
N # .. read in the next 2 lines
N
d # .. delete the pattern space
} # end of script
However, this script fails in cases such as the one given above, since
when lines 3: and 4: are read into the pattern space, the first test
fails and both lines are output. The SED script will skip line 4: since
the N command read it already, and program flow proceeds to line 5:.
The result is that line 4: (which should have been deleted) is not
deleted from the output.
Since blank lines can appear anywhere in the input file, there may be
two or even 3 blank lines before the deletion block. I have tried
writing other tests for the next two lines after a blank line, but the
problem is that SED program logic jumps to the NEXT line read after the
last "N" command rather than iterating on each line.
If anyone can offer some suggestions about resolving this problem using
SED, I would certainly appreciate it.
Some reflections. I have thought of two potential solutions to solve
difficulties such as this, which would (to my knowledge) require
expanding SED's pattern matching abilities. Perhaps for GNU wizards?
(1) offering MINIMAL matching for two addresses. The command
/^$/,/zzzz/ d
will delete lines 3-7, inclusive, since SED range addressing is
a maximal match. An option for minimal range addressing would
delete the lines 4-7 only.
(2) offering multi-line direct addressing. For example:
/^\nxxxx\nyyyy\zzzz$/ d # one possible syntax
/^$\n^xxxx$\n^yyyy$\n^zzzz$/ d # another possible syntax
One of these suggested syntaxes, which would not require using
the "N" function (and thus skip other lines of input), would be
helpful for resolving problems such as these.
I would appreciate any feedback or reflections on the above. Thanks.
If I should be posting this query in a different newsgroup, please let me
know; this is the closest match I could come to. (E-mail replies okay.)
--
Eric Pement <epement@ripco.com>
Technical Details
Message-ID:
<DGtuuq.AJ@rci.ripco.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1025b4,6054e8d9ed3d2572,start X-Google-Attributes: gid1025b4,public X-Google-Thread: 107393,6054e8d9ed3d2572,start X-Google-Attributes: gid107393,public From: epement@ripco.com (Eric Pement) Subject: SED q. re multiline patterns (long) Date: 1995/10/22 Message-ID: <DGtuuq.AJ@rci.ripco.com> X-Deja-AN: 117889794 sender: usenet@rci.ripco.com (Net News Admin) x-nntp-posting-host: foley.ripco.com x-ident-sender: epement organization: Ripco BBS, Free Trial account (312) 665-0065 newsgroups: comp.editors,gnu.misc.discuss