Path: csiph.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Ed Morton Newsgroups: comp.lang.awk Subject: Re: FIELDWIDTHS helper Date: Sun, 10 Aug 2025 14:25:33 -0500 Organization: A noiseless patient Spider Lines: 28 Message-ID: <107arnd$23gng$1@dont-email.me> References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 10 Aug 2025 19:25:33 +0000 (UTC) Injection-Info: dont-email.me; posting-host="eb005b0d92f38b43d4975ac27a100b03"; logging-data="2212592"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Iohr3droJFgEk+9YYNlua" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:lZB8MlFPwbx142DH5VSfi3G8Qno= X-Antivirus: Avast (VPS 250810-4, 8/10/2025), Outbound message Content-Language: en-US X-Antivirus-Status: Clean In-Reply-To: Xref: csiph.com comp.lang.awk:10005 On 8/6/2025 4:23 AM, Bruce wrote: > The other day I found myself unable to count field sizes accurately so I > wrote this little helper. > > NR == 1 { # Pick a line >     print >     nf = split(FIELDWIDTHS, fw) >     for (i = 1; i <= nf; i++) >         for (j = 1; j <= fw[i]; j++) >             printf "%d", (i % 10) >     print "" > } FWIW you could do that without nested loops and without using FIELDWIDTHS so it'll work in any POSIX awk, not just gawk: NR == 1 { # Pick a line for (i = 1; i <= NF; i++) { out = sprintf("%*s", length($i), "") gsub(/ /, i%10, out) printf "%s", out } print "" } Regards, Ed.