net.unix

merlyn at sequent.UUCP merlyn at sequent.UUCP
Tue Oct 9 04:43:50 AEST 1984


> From: berge at stolaf.UUCP
> Message-ID: <1977 at 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 at 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]



More information about the Comp.unix mailing list