"cut" needed to run CC

Leo de Wit leo at philmds.UUCP
Sun Sep 18 21:48:24 AEST 1988


In article <911 at riddle.UUCP> domo at riddle.UUCP (Dominic Dunlop) writes:
    [lines deleted]...
>Quick hack fix: echo "How are you today" | awk -d" " '{print $1 " " $3}'
    [more lines deleted]...
>It is left as an exercise for the reader to perform the operation above
>using sed.  Clue: it ain't pretty...

Not so pretty as awk, but not as ugly as some nroff scripts I've seen 8-).
Testing an sed-version however revealed it was almost 4 times as fast as
the awk version.
Consider:

------------ start here for awk version --------------
#!/bin/sh
# cut by awk

exec awk -d" " '{print $1 " " $3}' $*
------------ end   here --------------

and

------------ start here for sed version --------------
#!/bin/sh
# cut by sed

sp='  *'
wd='[^ ][^ ]*'

exec sed "s/$sp$wd$sp\\($wd\\).*/ \\1/" $*
------------ end   here --------------

On a source text (a News mailbox) of about 180Kbyte this was the result:

awk-version
       44.1 real        41.4 user         1.8 sys
sed-version
       12.3 real        10.5 user         1.1 sys

Output was redirected to /dev/null; directing it to a file will probably
increase both real and sys time slightly.
If you are going to use it with a compiler, you'd better make sure it's
fast, considering how often and for how large inputs it is going to be
used.  Maybe it's even worth considering writing a small C program for
it, although I doubt this will gain much over the sed version.

           Leo.

B.T.W. Although awk has builtin mechanisms for handling words ($1 etc),
it is pretty disappointing here. Anyone cares to check out perl on this one?



More information about the Comp.unix.questions mailing list