9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Rudolf Sykora <rudolf.sykora@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] awk help; not plan9 matter
Date: Thu, 17 Sep 2009 19:03:59 +0200	[thread overview]
Message-ID: <a560a5d00909171003v2e566809h680e43d24816c79b@mail.gmail.com> (raw)
In-Reply-To: <56a297000909170716q6f2fe75dqf2abfbf89cb20265@mail.gmail.com>

2009/9/17 Noah Evans <noah.evans@gmail.com>:
> Since you're doing character processing rather than record processing,
> isn't C your best tool for the job here?
>
> This is what I whipped out YMMV:
>
> #include <u.h>
> #include <libc.h>
> #include <bio.h>
>
> char *str;
> int ntok;
>
> #define WHITESPACE(c)           ((c) == ' ' || (c) == '\t' || (c) == '\n')
>
> void
> chgtok(Biobuf *bin, Biobuf *bout)
> {
>        int seentok, waswhite, c;
>        seentok = 1;
>        waswhite = 0;
>
>        while((c = Bgetc(bin)) != Beof){
>                switch(c){
>                case ' ':
>                case '\t':
>                        if(!waswhite){
>                                seentok++;
>                                waswhite = 1;
>                        }
>                        break;
>                case '\n':
>                        seentok = 1;
>                        break;
>                default:
>                        waswhite = 0;
>                        if(seentok == ntok){
>                                Bprint(bout, str);
>                                while((c = Bgetc(bin)) != Beof)
>                                        if(WHITESPACE(c))
>                                                break;
>                                Bungetc(bin);
>                        }
>                        break;
>                }
>                Bputc(bout, c);
>        }
>        Bflush(bout);
> }
>
> void
> main(int argc, char **argv)
> {
>        Biobuf bin, bout;
>
>        ARGBEGIN{
>        }ARGEND;
>        if(argc != 2)
>                sysfatal("usage");
>        ntok = atoi(argv[0]);
>        str = argv[1];
>        Binit(&bin, 0, OREAD);
>        Binit(&bout, 1, OWRITE);
>        chgtok(&bin, &bout);
>        exits(0);
> }
>

Thanks, terrific job. :)
But awk finally works (see my post at 12:46 or so) just fine and the
code is just straightforward 9 lines.
The only problem was to realize how one can proceed, i.e. here to
remember the individual spaces and reconstruct the line from the
fields.

Nonetheless, your solution is almost surely faster.

Thanks
Ruda



      parent reply	other threads:[~2009-09-17 17:03 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-17  8:23 Rudolf Sykora
2009-09-17  8:37 ` matt
2009-09-17  8:52   ` Rudolf Sykora
2009-09-17  9:04 ` matt
2009-09-17  9:12   ` Rudolf Sykora
2009-09-17 10:42     ` Rudolf Sykora
2009-09-17 12:35       ` yy
2009-09-17 12:55         ` Rudolf Sykora
2009-09-17 15:51     ` Lyndon Nerenberg - VE6BBM/VE7TFX
2009-09-17 16:02       ` erik quanstrom
2009-09-17 16:20         ` roger peppe
2009-09-17 16:55         ` Rudolf Sykora
2009-09-17 17:04           ` erik quanstrom
2009-09-17 17:10             ` erik quanstrom
2009-09-17 17:19               ` Rudolf Sykora
2009-09-17 17:34                 ` roger peppe
2009-09-17 20:33                   ` Rudolf Sykora
2009-09-17 20:46                     ` erik quanstrom
2009-09-17 21:09                       ` Rudolf Sykora
2009-09-17 21:22                         ` Rudolf Sykora
2009-09-17 21:24                         ` erik quanstrom
2009-09-17 14:16 ` Noah Evans
2009-09-17 14:45   ` roger peppe
2009-09-17 17:03   ` Rudolf Sykora [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a560a5d00909171003v2e566809h680e43d24816c79b@mail.gmail.com \
    --to=rudolf.sykora@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).