The other day I found myself unable to count field sizes accurately so I wrote this little helper.
NR == 1 { # Pick a line
nf = split(FIELDWIDTHS, fw)
for (i = 1; i <= nf; i++)
for (j = 1; j <= fw[i]; j++)
printf "%d", (i % 10)
print ""
}
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
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:
But I suspect that the program is intended to be a helper
when developing a program that uses FIELDWIDTHS.
Your portable example is no longer testing/displaying what the value of >FIELDWIDTHS is doing against the input data.
In article <20250810124827.931@kylheku.com>,
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
...
But I suspect that the program is intended to be a helper
when developing a program that uses FIELDWIDTHS.
Your portable example is no longer testing/displaying what the value of >>FIELDWIDTHS is doing against the input data.
It is pretty clear that Mr. Ed completely missed the point of the post.
He should not feel bad about that. It is pretty obscure. It took me a few reads to get it. I was half-way though posting a "WTF is this?" post in response, when it dawned on me what the point of it was.
The other day I found myself unable to count field sizes accurately so I wrote this little helper.
NR == 1 { # Pick a line
nf = split(FIELDWIDTHS, fw)
for (i = 1; i <= nf; i++)
for (j = 1; j <= fw[i]; j++)
printf "%d", (i % 10)
print ""
}
Example:
with FIELDWIDTHS = "1 3 5"
Output:
A dummy line from a file
122233333
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,064 |
Nodes: | 10 (0 / 10) |
Uptime: | 163:53:28 |
Calls: | 13,691 |
Calls today: | 1 |
Files: | 186,936 |
D/L today: |
9,181 files (2,736M bytes) |
Messages: | 2,411,516 |