[comp.unix.questions] First-line-only editing, part 2: Portable solutions

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (10/25/90)

As shown in my previous message, sed is much slower than a head-sed-cat
combination on a particular 300K file. However, the solution I proposed,
namely

  head -1 | sed 's/.../.../'; cat

is not portable. Apparently some people's versions of head don't seek
before they die, though the clone version I'm using certainly does.

So the fastest portable solution I've seen is

  head -1 file | sed 's/.../.../'; tail +2 file

which still runs up to 12x faster than sed '1s/.../.../' on very long
files, with a cutoff around 40K on this Sun 4.

(Note that you need to do a bit more work if your input is a pipe,
though this is not covered by ``file'' in the subject line. One simple
yet fast solution is to shuttle the output to a temporary file, then
apply the above.)

---Dan

daveb@nostromo.austin.ibm.com (Dave Burton) (10/26/90)

In article <16471:Oct2420:13:4390@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
|X-Original-Subject: Re: edit first line of long file
|As shown in my previous message, sed is much slower than a head-sed-cat
|combination on a particular 300K file. ...

Not (necessarily) so.

| So the fastest portable solution I've seen is
|  head -1 file | sed 's/.../.../'; tail +2 file

Try:

sed 's/.../.../;1q' < file > outfile

Nuff said.
--
Dave Burton			    | "You can lead a moron to water,
daveb@nostromo.austin.ibm.com	    |  but you can't make him think."
cs.utexas.edu!ibmchs!nostromo!daveb | -- stevea@i88.isc.com

mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) (10/31/90)

In article <4031@awdprime.UUCP> daveb@bach.austin.ibm.com (Dave Burton) writes:
:In article <16471:Oct2420:13:4390@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
:|X-Original-Subject: Re: edit first line of long file
:|As shown in my previous message, sed is much slower than a head-sed-cat
:|combination on a particular 300K file. ...
:
:Not (necessarily) so.
:
:| So the fastest portable solution I've seen is
:|  head -1 file | sed 's/.../.../'; tail +2 file
:
:Try:
:
:sed 's/.../.../;1q' < file > outfile
:
:Nuff said.
:--
:Dave Burton			    | "You can lead a moron to water,
:daveb@nostromo.austin.ibm.com	    |  but you can't make him think."
:cs.utexas.edu!ibmchs!nostromo!daveb | -- stevea@i88.isc.com


Outfile then only contains first line of file,  not the entire file
with the first line altered.

daveb@nostromo.austin.ibm.com (Dave Burton) (11/09/90)

In article <16471:Oct2420:13:4390@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
|As shown in my previous message, sed is much slower than a head-sed-cat
|combination on a particular 300K file. ...
| So the fastest portable solution I've seen is
|  head -1 file | sed 's/.../.../'; tail +2 file

In article <4031@awdprime.UUCP> daveb@bach.austin.ibm.com (Dave Burton) writes:
|[ no, try: ]
|sed 's/.../.../;1q' < file > outfile

which, of course, doesn't output the entire file.  Therefore, it seems that
the "fastest portable solution" [we've] seen is:

sed 's/.../.../;1q' file; tail +2 file
	or
(sed 's/.../.../;1q'; tail +2) < file

Now, truly, nuff said. :-)
--
Dave Burton
inet: daveb@bach.austin.ibm.com
uucp: cs.utexas.edu!ibmchs!auschs!nostromo!daveb

daveb@nostromo.austin.ibm.com (Dave Burton) (11/09/90)

In article <687@npdiss1.StPaul.NCR.COM> mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) writes:
|In article <4031@awdprime.UUCP> daveb@bach.austin.ibm.com (Dave Burton) writes:
|:sed 's/.../.../;1q' < file > outfile
|
|Outfile then only contains first line of file,  not the entire file
|with the first line altered.

Ouch!  Well, it _does_ edit only the first line. :-)
This line should work much better:

sed '1s/.../.../' < file > outfile
--
Dave Burton
inet: daveb@bach.austin.ibm.com
uucp: cs.utexas.edu!ibmchs!auschs!nostromo!daveb

cudcv@warwick.ac.uk (Rob McMahon) (11/11/90)

In article <4152@awdprime.UUCP> daveb@bach.austin.ibm.com (Dave Burton) writes:
>sed 's/.../.../;1q' file; tail +2 file
>	or
>(sed 's/.../.../;1q'; tail +2) < file

Quack, quack, oops.  This second one doesn't work.  Sed buffers its input and
a bufferful goes missing.  If it didn't a line goes missing.

>Now, truly, nuff said. :-)

Sorry about that.

Rob
--
UUCP:   ...!mcsun!ukc!warwick!cudcv	PHONE:  +44 203 523037
JANET:  cudcv@uk.ac.warwick             INET:   cudcv@warwick.ac.uk
Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England