First-line-only editing, part 2: Portable solutions

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Thu Oct 25 06:13:43 AEST 1990


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



More information about the Comp.unix.questions mailing list