Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!albanycs!crdgw1!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.questions Subject: Re: Help a novice: Will "sed" do? Keywords: Automatic edits. Sed. Message-ID: <2895@solo8.cs.vu.nl> Date: 17 Jul 89 18:35:19 GMT References: <2180@umbc3.UMBC.EDU> Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 53 rostamia@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` []" >&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@cs.vu.nl, mcvax!botter!maart