Help a novice: Will "sed" do?

Maarten Litmaath maart at cs.vu.nl
Tue Jul 18 04:35:19 AEST 1989


rostamia at umbc3.UMBC.EDU (Dr. Rouben Rostamian) writes:
\I need a command or a script that searches a text file for a given
\word or pattern and prints out all paragraphs that contain that word
\or pattern.  Paragraphs are blocks of text separated by one or 
\more blank lines.

----------8<----------8<----------8<----------8<----------8<----------
#!/bin/sh

test $# = 0 && {
	echo "Usage: `basename $0` <pattern> [<files>]" >&2
	exit 1
}

pattern=`echo "$1" | sed 's-/-\\\\/-g'`		# quote slashes in the pattern
shift

tab="`ctrl I`"		# if you don't have `ctrl': tab="	" (a hard tab)
empty="^[ $tab]*$"	# the pattern of an `empty' line

SED="
: gap
	/$empty/!b para
: gap1
	n
	b gap
: para
	/$pattern/b found
	H
	n
	/$empty/!b para
: cleanup
	s/.*//
	x
	b gap1
: found
	H
	\${
		g
		p
	}
	n
	/$empty/!b found
	g
	p
	b cleanup
"

sed -n "$SED" $*
----------8<----------8<----------8<----------8<----------8<----------
-- 
   "... a lap-top Cray-2 with builtin    |Maarten Litmaath @ VU Amsterdam:
cold fusion power supply"  (Colin Dente) |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.questions mailing list