From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 In-Reply-To: References: Date: Thu, 17 Sep 2009 16:16:45 +0200 Message-ID: <56a297000909170716q6f2fe75dqf2abfbf89cb20265@mail.gmail.com> From: Noah Evans To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [9fans] awk help; not plan9 matter Topicbox-Message-UUID: 709c7d10-ead5-11e9-9d60-3106f5b1d025 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 #include #include char *str; int ntok; #define WHITESPACE(c) ((c) =3D=3D ' ' || (c) =3D=3D '\t' || (c) =3D=3D '\n= ') void chgtok(Biobuf *bin, Biobuf *bout) { int seentok, waswhite, c; seentok =3D 1; waswhite =3D 0; while((c =3D Bgetc(bin)) !=3D Beof){ switch(c){ case ' ': case '\t': if(!waswhite){ seentok++; waswhite =3D 1; } break; case '\n': seentok =3D 1; break; default: waswhite =3D 0; if(seentok =3D=3D ntok){ Bprint(bout, str); while((c =3D Bgetc(bin)) !=3D Beof) if(WHITESPACE(c)) break; Bungetc(bin); } break; =09 } Bputc(bout, c); } Bflush(bout); } void main(int argc, char **argv) { Biobuf bin, bout; ARGBEGIN{ }ARGEND; if(argc !=3D 2) sysfatal("usage"); ntok =3D atoi(argv[0]); str =3D argv[1]; Binit(&bin, 0, OREAD); Binit(&bout, 1, OWRITE); chgtok(&bin, &bout); exits(0);=09 } On Thu, Sep 17, 2009 at 10:23 AM, Rudolf Sykora w= rote: > 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 > =A0awk '{$2=3D"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 =A0 =A0 =A03 4 =A0 =A0 =A0 =A0 =A0 8 > changes to > 1 hell 4 8 > while I need > 1 =A0 =A0 =A0hell 4 =A0 =A0 =A0 =A0 =A0 8. > > Any help? > Thanks > Ruda > >