• Syntactic Sugar (Was: The Art of Unix Programming - Case Study: awk)

    From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Fri Feb 11 13:59:31 2022
    From Newsgroup: comp.lang.awk

    In article <20220209235134.861@kylheku.com>,
    Kaz Kylheku <480-992-1380@kylheku.com> wrote:
    Sure, but you don't get to use pattern/action pairs on the result.

    But that's largely just syntactic sugar for a glorified case statement.

    Instead of

    /abc/ { ... }
    $2 > $3 { ... }

    you have to write

    if (/abc/) { ... }
    if ($2 > $3) { ... }

    kind of thing.

    Of course, it can be (and often has been) argued that everything in any programming language is just "syntactic sugar" for the underling machine
    code. Everything is really just for programmer convenience.

    Personally, I tend to agree with Ben here, that not being able to use the "automatic input loop" (aka, the "pattern/action" facility for which AWK is justly famous) on non-traditional input is a Bad Thing. It would be nice
    if this were not the case.

    In fact, since I like to write my AWK programs as "true AWK programs"
    rather than as shell scripts - i.e., my scripts start with:

    #!/path/to/gawk -f

    rather than:

    something | gawk ...

    And I want to be able to use the pattern/action facility on piped-in input
    (and also output - see below), I have written an extension library for GAWK that enables that. That is, I can do:

    @load "pipeline"
    BEGIN { pipeline("in","Some Shell Command Here") }
    /foo/ { bar...}

    and it reads from the pipeline as if it had been piped-in from the shell.

    I also find it annoying to have to put "| cmd" at the end of every command
    that generates output - when I want the output to go to a pipe (e.g.,
    "less"). So, I can do:

    @load "pipeline"
    BEGIN { pipeline("out","less") }
    { print }

    With the expected result.
    "He is exactly as they taught in KGB school: an egoist, a liar, but talented - he
    knows the mind of the wrestling-loving, under-educated, authoritarian-admiring white male populous."
    - Malcolm Nance, p59. -
    --- Synchronet 3.19b-Linux NewsLink 1.113