Re: sed and awk (Was: The "asort" extension (Was: feature proposal: expand ...

Stan Marsh <[email protected]>
Newsgroups gmane.comp.shells.bash.bugs
Message-ID <[email protected]>
From:	Zachary Santer
Subject: sed and awk (Was: The "asort" extension (Was: feature proposal: expand ...) lol)
Date:	Wed, 26 Aug 2026 21:23:43 -0400


>On Wed, Aug 26, 2026 at 8:07'AM Stan Marsh <[email protected]> wrote:
>>
>> From:   Zachary Santer
>> Subject: Re: The "asort" extension (Was: feature proposal: expand associative 
>> arrays in lexicographic order ...)
>> Date:   Wed, 26 Aug 2026 01:19:55 -0400
>>
>> >This script is all about running commands and filtering their output
>> >through sed and awk. It makes sense as a bash script. If there's a
>> >more powerful programming language that can do the equivalent of
>>
>> Whenever I see the phrase "sed and awk", I immediately think "No need
>> for sed; whatever sed can do, AWK can do and do better".

>None of the commands' output gets filtered through both. It's one or the other.

That's good to know.  So, no monstrosities to fret about.

>I was struck by a couple things while learning AWK programming last year:

Here are my comments, mostly defending GAWK, but yeah, there are some
points against it as well.

>1. gensub() should return the number of replacements made, like sub()
>and gsub() do. [1]

>What was so bad about modifying the target variable in place?

IMHO, the primary advantage of gensub() *is* that it returns the
modified string, rather than modifying it in place.  Quite often, that
is what you want - you want to do something immediately with the
result rather than have to do it in 2 steps.  Admitedly, this makes it
difficult to check if anything actually happened - i.e., if the result
was different than the original.   However, this idiom actually does
work:

    $0 != $0 = gensub(/.../,"...",1) { ... }

This does the gensub(), assigns the result back to $0 and checks to
see if the new $0 is different from the old one and executes the block
if so.

>When using sed, I quite often do 's/pattern/replacement/p' to print
>the pattern space if a substitution was made. I also quite often use
>\n references in the replacement string. In an AWK script, I can only
>do one or the other before it starts to get ugly.

See above.

>2. No "local" keyword? Really? [2]

>All the local variables you'll ever need in a function have to be
>declared as arguments to that function, and you just don't assign
>values to those arguments when you call the function. Just... okay?
>Doesn't mean it has to stay like that forever.

Admitedly, the syntax for declaring local variables in a function is
kinda dorky.  But that's just historical precedent.  As it happens,
there *is* a "local" keyword, that behaves as you'd expect - in a
certain other AWK implementation that I cannot name here.

It is not inconceivable that GAWK could eventually implement "local",
but it is not there now.

>Not like sed supports user-defined functions to begin with.

Right.

>I learned AWK programming because I was looking at some complex sed
>expressions with { } command groups, b unconditional branches, and
>whatnot. I wasn't happy with them and I want this script to be
>intelligible to my coworkers. The AWK scripts that are there now are a
>huge improvement. So yes, each has its place in my repertoire.

The CW in the AWK group(s) is that once you get past "s/foo/bar/p" in
sed, you should switch to AWK.  Going any further with sed is just
asking for pain and suffering.  The general feeling is that few people
are even aware that "sed" does have a programming language in it - and
that that (that few people know it) is a Good Thing.

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

When responding to my posts, please try to refrain from giving bureaucratic
answers.  If you have nothing useful to say, then just click Next and go on.

BTW, ain't interested in kludgey workarounds - got enough of my own!
(YT id: 6H1lcubN0oE)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.