[net.unix] net.unix

berge@stolaf.UUCP (Eric Berge) (10/06/84)

{}

	I was wondering if this is a chronic problem with sed.  It
seems that I cannot get sed to match patterns with an imbedded newline.
I have talked to some other avid users of sed and they seem to have
the same problem.  According to the documentation, the sequence '\n'
will match a newline imbedded in a string.  So this is what I tried:

	I wanted to match:

		.ul
		followed by anything on the next line

	and convert it to

		\fIfollowed by anything on the next line\fR

	so I tried:

		sed 's/^\.ul\n\(.*\)$/\\fI\1\\fR/'

	I even tried simple examples to try to get it to work, but to
no avail.  Can anyone help me out?

-- 

	"That which does not kill, can only make us stronger."

					-- Nietzsche

	Eric Berge

	...{ihnp4|decvax}!stolaf!berge

willcox@ccvaxa.UUCP (10/08/84)

The problem is that sed only reads in one line at a time into its "pattern
space", so there are never any embedded newlines.  There are a number of
different commands that let you play with what is in the pattern space and
"hold space".  The man page describes them.  Your problem, for example,
could be done by putting the following into a file:

	/^\.ul/{
		N
		s/^.*\n/\\fI/
		s/$/\\fP/
	}

and then executing with the line "sed -f <scriptfile> <datafile>".  (I find
it much easier to use sed script files than putting it all on the command
line.)

merlyn@sequent.UUCP (10/08/84)

> From: berge@stolaf.UUCP
> Message-ID: <1977@stolaf.UUCP>
> Date: Fri, 5-Oct-84 17:06:20 PDT
> 
> 	I was wondering if this is a chronic problem with sed.  It
> seems that I cannot get sed to match patterns with an imbedded newline.
> I have talked to some other avid users of sed and they seem to have
> the same problem.  According to the documentation, the sequence '\n'
> will match a newline imbedded in a string.  So this is what I tried:
> 	I wanted to match:
> 		.ul
> 		followed by anything on the next line
> 	and convert it to
> 		\fIfollowed by anything on the next line\fR
> 	so I tried:
> 		sed 's/^\.ul\n\(.*\)$/\\fI\1\\fR/'
> 	I even tried simple examples to try to get it to work, but to
> no avail.  Can anyone help me out?

Yeah, I can help you out.  Which way did you come in? :-)
[it's monday folks!]

Embedded newlines ONLY show up in the pattern space when you do something
fancier than just s/this/that/ stuff.  One way is if you substitute
them in, like
	s/this/that\
	and that/
in which case, a later /that\nandthat/ will match what's in the pattern
space.  Remember, sed works on a LINE AT A TIME unless you tell it
otherwise.  A solution to your problem might take the form of:
	sed -n '
	/^\.ul/{
		n			(discard current line; fetch next)
		s/.*/\\fI&\\fR/		(insert nroff blather)
	}
	p'				(print regardless)
Yeah, not an easy solution, but it works (oops.. untested though... just
relying on old information!).  The reason for the sed -n and then
the later p is a bit esoteric... you don't want the 'n' command to print
the '.ul', and using a 'd' inside curly braces aborts the current cycle
too early.  (Skip that if you can't follow it... just comes from having
written rather LONG sed scripts.)

Write me directly if you want further help.  I can get long-winded about
UNIX language (yes, sed is a language! :-) solutions.

-- A particularly personal and original observation from the thought-stream of
Randal L. ("s/nothing/something/p") Schwartz, esq. (merlyn@sequent.UUCP)
	(Former Official Legendary Sorcerer of the 1984 Summer Olympics)
Sequent Computer Systems, Inc. (503)626-5700 (sequent = 1/quosine)
UUCP: {decwrl,ogcvax,pur-ee,rocks34,shell,unisoft,vax135,verdix}!sequent!merlyn
Original Material (C) 1984 by Randal L. Schwartz [ALL RIGHTS RESERVED]

mark@umcp-cs.UUCP (Mark Weiser) (10/09/84)

>	I was wondering if this is a chronic problem with sed.  It
>seems that I cannot get sed to match patterns with an imbedded newline.
>I have talked to some other avid users of sed and they seem to have
>the same problem.  According to the documentation, the sequence '\n'
>will match a newline imbedded in a string.  So this is what I tried:
>	I wanted to match:
>		.ul
>		followed by anything on the next line
>	and convert it to
>		\fIfollowed by anything on the next line\fR
>	so I tried:
>		sed 's/^\.ul\n\(.*\)$/\\fI\1\\fR/'
>	I even tried simple examples to try to get it to work, but to
>no avail.  Can anyone help me out?
>
Sure.  The problem is that ordinarily sed only puts single lines in
its "pattern space".  To get imbedded new lines in there you must
use the sed "N" command.  To quote from the sed manual entry:
	N  Append the next line of input to the pattern space 
	   with an embedded newline.	
Here is a sed script that does what you want. It takes a slightly 
more complicated form, worthy of placing in a file rather than on the 
sed command line:

/^\.ul$/N
s/^\.ul\n\(.*\)$/\\fI\1\\fR/

-- 
Spoken: Mark Weiser 	ARPA:	mark@maryland
CSNet:	mark@umcp-cs 	UUCP:	{seismo,allegra}!umcp-cs!mark

medin@ucbvax.ARPA (Milo Medin) (04/21/85)

Folks, I'm trying to track down some office automation software
made by a company called quadratron.  Anyone have any idea as to how
to reach them (mail address, phone, etc..) ?  Anyone have a favorite
package that is 1-2-3 like?  I personally don't like this stuff, but
some users of mine wanted me to inquire about it.

					thanks,
					  Milo