sed - match newlines on input

ras1 at mtuxo.UUCP ras1 at mtuxo.UUCP
Thu Mar 12 05:00:36 AEST 1987


In article <570 at hao.UCAR.EDU>, bill at hao.UCAR.EDU (Bill Roberts) writes:
>> I'm trying to match a pattern over multiple lines ... with the sed script:
>>      s/one\ntwo\nthree/one, two, three/g
>> one would expect to get the output: one, two, three

In article <572 at custom.UUCP> boykin at custom.UUCP (Joseph Boykin) writes
>The documentation on this point within the SED documentation is
>definately confusing, when we did the documentation for PC/SED,
>...
>To be honest, I don't feel like mucking with SED long enough
>to give you a script to do what you want (someone else probably will!)

Okay, here goes:

## FIRST a relatively clean straight forward "sed -f Script":

/^abc$/!d
h
n
/^123$/!d
H
n
/^xyz$/!d
H
g
s/\n/, /g
p

## NEXT a couple of "tighter" versions:

sed -n "/^abc$/{h; n
            /^123$/{H; n; H
                /^xyz$/g; s/\n/, /gp;}
       }" $*    ## Above "s...gp" only prints if both the 'H' AND 'g' click

#OR: 

sed -n "/^abc$/!d; h; n; /^123$/{H; n; /^xyz$/!d; x; G; s/\n/, /gp;}" $*

## LAST a grotesque (Byzantine?) WORKING variant
#  is provided (with full apologies in advance) only as
#  POSSIBLE food for thought, discussion and reflection:
#        (or nausea and revulsion but PLEASE !flame:-)

CHUNK="/^abc$/{!d; h; n; /^123$/{H; b outside
       :within
       p;}"

sed -n "#Note:  The 's' and 't' won't click without prior 'h', 'H' AND 'x'
        $CHUNK
        }
        d
        :outside
        n; /^xyz$/{x; G;}
        s/\n/, /g
        t within
        " $*

#Points for consideration:
#       + Quoted multi-line sed script without "-e" (only needed icw "-f").
#       + Leading "#" comment within script.
#	+ "sed" script CHUNK substituted from the shell environment.
#       + Multiple sed commands per a line.
#       + Multiple progressive pattern matching elements on a line.
#       + Control grouping '{' braces:
#               + Nested,
#               + Multiple opens on a line,
#               + Multi-line control groups.
#       + Branching 'b' (and conditional branching 't') to labels that are
#         "outside" AND also back "inside" of control group '{' constructs.
#       + The LITERAL case "^one\ntwo\nthree$"
#         could be handled (matched & output)
#         WITHOUT the use of the hold area.

Enuf sed?/Go4 sam?-)

Dick Stewart; ihnp4!tarpon!stewart; ATT-IS: DISCLAIMER ...

PS:  Aficionados of "awk" try some "time"d comparisons;
     Although limited "sed" is quick.
     All of the sed scripts above were run on 3B's and a PC7300.  



More information about the Comp.unix.questions mailing list