2009/9/17 Noah Evans <noah.ev...@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