Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!cbatt!ihnp4!homxb!houxm!mtuxo!ras1 From: ras1@mtuxo.UUCP Newsgroups: comp.unix.questions Subject: Re: sed - match newlines on input Message-ID: <2541@mtuxo.UUCP> Date: Wed, 11-Mar-87 14:00:36 EST Article-I.D.: mtuxo.2541 Posted: Wed Mar 11 14:00:36 1987 Date-Received: Fri, 13-Mar-87 06:18:38 EST References: <570@hao.UCAR.EDU> Organization: AT&T Information Systems Labs, Holmdel NJ Lines: 81 Summary: Mucking with embedded newlines may be easier than you think! In article <570@hao.UCAR.EDU>, bill@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@custom.UUCP> boykin@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.