sed help results

Eric Berge berge at stolaf.UUCP
Thu Oct 18 10:04:54 AEST 1984


{}

I posted the following article for help with a sed script.  Several
people have requested that I post the answers which I recieved:

>		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?

The problem was that sed cannot match a newline imbedded in a pattern space,
unless you specifically tell sed to join two lines of text so that the
pattern space will have a newline imbedded in it.  The key in all responses
was the use of the 'N' command to append the next pattern space to the
current one and them make the search for the imbedded pattern space and
make the appropriate substitution.

The following are some of the various sed scripts which I received to do
the job:

>		sed -e '/^\.ul$/N' -e 's/^\.ul\n\(.*\)$/\\fI\1\\fR/' <inp >out
>	_____________________________________________________
>	Matt		University	crawford at anl-mcs.arpa
>	Crawford	of Chicago	ihnp4!oddjob!matt


#	This is not a problem with sed.
#	You've got to get the second line of text into the pattern space 
#	before trying your replace command.
#	The following should do it:
#
#	sed -e '/^\.ul$/{' -e 'N' -e 's/...\n\(.*\)/\\fI\1\\fR/' -e '}'
#
#	steve roush
#	attis - denver
#	druxw!csr
#	303-538-4860


>	sed '/^\.ul$/{
>		N
>		s/.*\n\(.*\)/\\fI\1\\fR/
>		}'
>
>	Alan Bland, druxp!mab, AT&T-ISL Denver


#	/^\.ul$/ {
#		N
#		s/^\.ul\n/\\fI/
#		s/$/\\fR/
#	}
#
#
#	Lee McMahon
#	Bell Labs, Murray Hill
#	...ihnp4!research!lem


>	sed -n '
>	/^\.ul/{
>		n			(discard current line; fetch next)
>		s/.*/\\fI&\\fR/		(insert nroff blather)
>	}
>	p'				(print regardless)
>
>	Sequent Computer Systems, Inc. (503)626-5700 (sequent = 1/quosine)
> {decwrl,ogcvax,pur-ee,rocks34,shell,unisoft,vax135,verdix}!sequent!merlyn
>	Original Material (C) 1984 by Randal L. Schwartz [ALL RIGHTS RESERVED]


#	/^\.ul$/N
#	s/^\.ul\n\(.*\)$/\\fI\1\\fR/
#	
#	-- 
#	Spoken: Mark Weiser 	ARPA:	mark at maryland
#	CSNet:	mark at umcp-cs 	UUCP:	{seismo,allegra}!umcp-cs!mark




>	     In  "SED - a Non-interactive Text Editor", section 2.2
>	item 4) describes \n as matching "an imbedded newline character,
>	but not the newline at the end of the pattern space".  Section 1.3
>	says "Ordinarily the pattern space is one line of the input text,
>	but more than one line can be read in...using the N command (Sec.3.6)."
>	So unless you use the N command you can't expect \n to match anything.
>	The reference to "3.6" (at least in my copy) is apparently a misprint
>	for 3.4.  I find that the following script using the N command
>	does what you want:
>		/^\.ul/{N
>		s/^\.ul\n\(.*\)/\\fI\1\\fP/
>		}
>	(I've replaced your \\fR with \\fP, which seems to make more
>	sense.)
>	Note that ex can get the same effect without any embedded-newline
>	facility, using the command
>		g/^\.ul/d|s/.*/\\fI&\\fR
>	The point is that after a "d", the current line is the line after
>	the one that was deleted, so the indicated substitution will be
>	performed on that line.  I would very much like multiple-line
>	pattern-recognition capability in ex, but it isn't needed for this.
>	I don't really know sed, but since the documentation says that
>	after deletion no further commands are performed "on the corpse
>	of the current line...the list of editing commands is restarted
>	from the beginning" (section 3.1) there is no evident way to achieve
>	the same result without using N and \n.
>	
>	George Bergman
>	From: decvax!ucbvax!ucbtopaz:gbergman


Thanks to everyone who responded so readily.

-- 

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

					-- Nietzsche

	Eric Berge

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



More information about the Comp.unix mailing list