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); } On Thu, Sep 17, 2009 at 10:23 AM, Rudolf Sykora <rudolf.syk...@gmail.com> wrote: > Hello, > > simple task. > I want to change the 2nd field on each line of a file, but preserve > the spacing of the lines. > > If I do > awk '{$2="hell"; print}' file > the field gets changed, but all the spacing of the lines is gone; i.e. > any space is now just ' ' like this: > 1 3 4 8 > changes to > 1 hell 4 8 > while I need > 1 hell 4 8. > > Any help? > Thanks > Ruda > >