• FIELDWIDTHS helper

    From Bruce@07.013@scorecrow.com to comp.lang.awk on Wed Aug 6 10:23:10 2025
    From Newsgroup: comp.lang.awk

    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 ""
    }


    Example:

    with FIELDWIDTHS = "1 3 5"

    Output:

    A dummy line from a file
    122233333
    --
    Bruce Horrocks
    Hampshire, England

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ed Morton@mortonspam@gmail.com to comp.lang.awk on Sun Aug 10 14:25:33 2025
    From Newsgroup: comp.lang.awk

    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.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.awk on Sun Aug 10 19:51:17 2025
    From Newsgroup: comp.lang.awk

    On 2025-08-10, Ed Morton <mortonspam@gmail.com> wrote:
    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:

    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.
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Sun Aug 10 21:34:43 2025
    From Newsgroup: comp.lang.awk

    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 randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL:
    http://user.xmission.com/~gazelle/Sigs/ForFoxViewers
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.awk on Mon Aug 11 04:10:16 2025
    From Newsgroup: comp.lang.awk

    On 2025-08-10, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    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.

    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."
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.lang.awk on Mon Aug 11 11:51:05 2025
    From Newsgroup: comp.lang.awk

    On 06.08.2025 11:23, Bruce wrote:
    The other day I found myself unable to count field sizes accurately so I wrote this little helper.

    It's nice, but it should at least be mentioned that it supports only
    the subset of GNU Awk's FIELDWIDTHS syntax variants as you depicted
    it; like "1 3 5". While the optional final '*' would be no issue -
    since the purpose is obviously to create the correct numbers for the FIELDWIDTHS variable - the optional "skip-space" specification might
    be an issue.

    For example; for you sample data "A dummy line from a file" I suppose
    you'd want to rather obtain a FIELDWIDTHS = "1 1:5 1:4 1:4 1:1 1:4";
    and the helper doesn't help you here. (Although an extension of your
    tool could be written.)

    Some of the confusion that was reported in this thread could likely
    have been alleviated by a better fitting data sample. Maybe

    FIELDWIDTHS = "1 5 4 4 1 4" # test spec

    ADummyLineFromAFile # input
    1222223333444456666 # output

    would have made the intention clearer.

    Janis


    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 ""
    }


    Example:

    with FIELDWIDTHS = "1 3 5"

    Output:

    A dummy line from a file
    122233333


    --- Synchronet 3.21a-Linux NewsLink 1.2