Help a novice: Will "sed" do?

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Jul 18 01:02:20 AEST 1989


In article <2180 at umbc3.UMBC.EDU> 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.

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.



More information about the Comp.unix.questions mailing list