Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.questions Subject: Re: how can I join a range of lines in sed? Keywords: sed join Message-ID: <1991Mar08.135025.25987@convex.com> Date: 8 Mar 91 13:50:25 GMT References: <1991Mar7.231612.12899@cbnewsl.att.com> Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 42 Nntp-Posting-Host: pixel.convex.com From the keyboard of psrc@mtunq.att.com (Paul S. R. Chisholm): :I have a file with sections inside BEGIN/END pairs. (In 99% of the :interesting cases, there are only either one or two lines between the :two dot lines, if that makes any difference.) I want to "join" (in the :sense of the ed/ex/vi command) the lines together, and delete the :sentinel lines. I could do this easily enough in awk, but the rest of :the job is done in a sed script, and I'd like to keep all the work :together if I can. I find it more difficult than it should be in sed to do relatively simple things. Awk's more traditional control structures seems to be easier on my brain. :Somehow, I need to use the sed N command to "append the next line of :input to the pattern space with an embedded newline." Without the N, :the pattern space consists of only one line at a time. I've tried :substitutions and the t command (yuchh, I haven't used an if/goto :statement in over a decade!), but it never quite worked; e.g, this: You might consider running your whole script through s2p, the sed-to-perl translator. This would yield you the more traditional control structures instead of sed's gotos and other oddities. For the part that you posted, this works: #!/usr/bin/perl -n if (/^BEGIN/ .. /^END/) { if (/^BEGIN/) { $hold = ''; } elsif (/^END/) { $hold =~ s/\n/ /g; $hold =~ s/ $//; print ".FOO \"$hold\"\n"; } else { $hold .= $_; } } else { print; } I just find this kind of thing easier to read than cryptic sed scripts. --tom