Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!purdue!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.unix.questions Subject: Re: Help a novice: Will "sed" do? Message-ID: <10540@smoke.BRL.MIL> Date: 17 Jul 89 15:02:20 GMT References: <2180@umbc3.UMBC.EDU> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 29 In article <2180@umbc3.UMBC.EDU> 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. It's pretty hard to do this with standard UNIX text-file utilities, because most of them work on a line-at-a-time basis. That means when you find the pattern, it's too late to output the preceding lines. A reasonable text editor (e.g. "sam") can do the job, along these lines: for entire file find next occurrence of pattern from current location forward to end of file if none done else find nearest blank line from current location backward to beginning of file if none move to beginning of file mark location find next blank line from current location forward to end of file if none move to end of file print lines from mark to current location The details of how to write such an editor script depend on the editor.