Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 49:51:12 |
Calls: | 583 |
Files: | 1,138 |
Messages: | 111,303 |
The other day I found myself unable to count field sizes accurately so I wrote this little helper.
NR == 1 { # Pick a line
-a-a-a print
-a-a-a nf = split(FIELDWIDTHS, fw)
-a-a-a for (i = 1; i <= nf; i++)
-a-a-a-a-a-a-a for (j = 1; j <= fw[i]; j++)
-a-a-a-a-a-a-a-a-a-a-a printf "%d", (i % 10)
-a-a-a 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
-a-a-a print
-a-a-a nf = split(FIELDWIDTHS, fw)
-a-a-a for (i = 1; i <= nf; i++)
-a-a-a-a-a-a-a for (j = 1; j <= fw[i]; j++)
-a-a-a-a-a-a-a-a-a-a-a printf "%d", (i % 10)
-a-a-a 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
On 2025-08-10, Kenny McCormack <gazelle@shell.xmission.com> wrote:
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.
Yeah; some people can't be bothered to communicate.
E.g.
"I have come up with a nice debugging aid that helps me fine-tune
the values of FIELDWDITHS to correctly match my input.
I simply take a stab at some FIELDWIDTHS values and then run
this code against some actual input.
The code prints the input record verbatim, then splits into fields
using the proposed FIELDWIDTHS, and finally prints a representation of
the field widths under the data to make it obvious what part of the data belongs to each field."