edit first line of long file

edit first line of long file

Post by Cameron Pai » Sat, 27 Oct 1990 00:44:44

Quote:>Plan B: pull off the first line with "line", pass it to sed, and
>somehow magically get lines 2-n sent to stdout with cat. [...]

Try something like:

        (line | sed 's/root/ROOT/'; cat) < /etc/passwd


...!{hplabs,mcvax,nttlab,ukc,uunet}!munnari!foster.avid.oz.au!cbp - UUCP


edit first line of long file

Post by Cameron Pai » Sat, 27 Oct 1990 01:58:20

something like:

Quote:>    (line | sed 's/root/ROOT/'; cat) < /etc/passwd

countered with:

Quote:>sed is the way.  it's not that much slower than cat (0.9
>seconds cpu vs. 0.0, but the perceived time was about a
>second either way and computed to 0:00).  [...]

and I felt a bit miffed. :-) So, I did a little experiment. The results
were interesting. I took a medium-sized file that many of you will

        2129132 Oct 26 01:45 /usr/local/lib/news/ctl/history

The top line of which looks something like:

        <stuff>   649108360~66386160       news.announce.conferences/494

and time(1)ed the following three commands:

(line | sed 's/announce/changed/'; cat) < /usr/local/lib/news/ctl/history

real     2:11.4
user        0.3
sys        18.9

sed '1s/announce/changed/' < /usr/local/lib/news/ctl/history

real     7:22.1
user     1:35.9
sys        30.8

(line | sed 's/announce/changed/'; cat -u) < /usr/local/lib/news/ctl/history

real     1:53.8
user        0.2
sys        19.9

If we ignore the third command (which is a minor variation on the first)
we find that the first is 3.36 (real) times faster and 320 (user) times
more efficient. I'm too lazy to trial it dozens of times on a variety of
hosts but I think you'll get the general idea...


...!{hplabs,mcvax,nttlab,ukc,uunet}!munnari!foster.avid.oz.au!cbp - UUCP


edit first line of long file

Post by Boyd Rober » Sat, 27 Oct 1990 09:02:25


>Huh? *Nothing* is going to run more quickly than cat, and programs that
>do any sort of processing are guaranteed to run more slowly. How about
>timing these things for yourself?

Don't be stupid.  It depends how cat is implemented.  I remember being
horrified to find that one verison of cat used stdio fread/fwrite. And
they were implemented as _two_ nested loops with a getc/putc in the
middle.  Was that _slow_ or what?

Gotta watch out for those `universal affirmitives' :-)

``When the going gets wierd, the weird turn pro...''


edit first line of long file

Post by Blair P. Hought » Sat, 27 Oct 1990 14:48:51

>>        (line | sed 's/root/ROOT/'; cat) < /etc/passwd

>countered with:

>>sed is the way.  it's not that much slower than cat (0.9

Countered?  I posted on _Monday_ evening, an easy three days before
you did.  If that's countering, I'd like to announce I'm countering
the price of oil next wednesday; it's going to climb like a rocket.

I'd completely forgotten line(1).

Apparently, I'm not the only one :-|.

                                  "Gotta go see a broker about
                                   some short-selling on the
                                   commodities floor..."


1. edit first line of long file

| > sed is the way.
| No, it's not. Try head -1 | sed 's/.../.../'; cat.

  Okay, results below.

|                                                            On this
| machine, sed is more than 12 times slower than cat.


Script started [y.tmp] at Tue Oct 23 23:03:14 1990
$ ls -l x.tmp
-rw-r--r--   1 davidsen users     143405 Oct 23 23:01 x.tmp
$ time cat x.tmp > /dev/null

real    0m0.50s
user    0m0.00s
sys     0m0.36s
$ time sed '1s/foo/bar/' x.tmp > /dev/null

real    0m2.20s
user    0m1.42s
sys     0m0.40s
$ time { head -1; sed 's/foo/bar/'; cat; } < x.tmp > /dev/null

real    0m3.16s
user    0m1.86s
sys     0m0.70s
$ exit
Script ended [y.tmp] at Tue Oct 23 23:05:28 1990

  How 'bout that! The overhead of all the other stuff made it slower. I
guess I'll vote for using just plain sed, because it's easier, faster,
uses less memory, etc. I didn't create a multi-megabyte file, so there
may be a break even point, but I wouldn't bet on it.

Other points:

  Yes, this was ksh, so the time reported is for all processes.
/bin/time would not do as well unless you started a subshell.

  Your 12:1 slower surprised the hell out of me, but you are definitely
in the right order of magnitude, perhaps even being a little nice to
sed. I really would have expected sed to just bypass the stuff after
line 1.

    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

2. Universal command options completion?

3. how to append one file's line to another file's line?

4. module util compilation (rev)

5. Editing a long line

6. DOS partitions - Is FIPS what I want?

7. Editing and reading the first and last line on a document.

8. Gnome-terminal transparency with KDE

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

10. Content of file is one line has to be displayed in seperated lines

11. Insert a line at the first line without a # in a file

12. Delete the first line of a 1.4 million lines file

13. Lines from a file, one-by-one