Re^2: sed ?

Maarten Litmaath maart at cs.vu.nl
Tue Dec 13 08:55:50 AEST 1988


Dave Lawrence writes:

\#!/bin/csh -f 
\ypcat hosts > /tmp/hosts
\cat /dev/null > /tmp/hunters

Why not simply `: > /tmp/hunters'?

\tail -f /tmp/hunters  & /* thrown in when I decided on doing it all in bg */
\@ line = 0
\@ loop = `wc -l /tmp/hosts`
\while(line < loop)
\	@ line = line + 1
\	**** see below for choices for this line ****
\	hunt -q $site |& grep -v pawl.rpi.edu >> /tmp/hunters
\end
\ 
\**** choices:
\	set site = `sed -n ${line}P /tmp/hosts | cut -f2 | cut -f2 -d" "`
\   or
\	set site = `tail +$line /tmp/hosts | head -1 | cut -f2 | cut -f2 -d" "`

Each time the search gets longer, which you won't want.

\Now silly me, I thought sed would be the faster one because all it would
\do would be to jump in and take only the line it needed (perhaps reading
\all the lines before it.)

And all the lines after it, if you don't tell sed to quit! :-(
This is a way to do it more efficiently:

	sed -n $line'{;p;q;}' /tmp/hosts

(Question aside: why isn't sed's `;' feature documented?)

However, you should - like in most cases - use a Bourne shell script:

	#! /bin/sh

	exec 3> /tmp/hunters$$		# file descriptor 3 now refers to
					# the outputfile
	tail -f /tmp/hunters$$ &

	ypcat hosts | cut -f2 | cut -f2 -d' ' |
	while read site
	do
		hunt -q $site 2>&1 | grep -v pawl.rpi.edu >&3
	done

The big plus of using a file descriptor is its stream-like nature;
the outputfile needn't be reopened every cycle. Needless to say it's VERY
convenient the Bourne shell lets you `pipe into a while loop'.
-- 
fcntl(fd, F_SETFL, FNDELAY):          |Maarten Litmaath @ VU Amsterdam:
      let's go weepin' in the corner! |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.wizards mailing list