sed and awk (Was: The "asort" extension (Was: feature proposal: expand ...) lol)
Zachary Santer <[email protected]>
| Newsgroups | gmane.comp.shells.bash.bugs,gmane.comp.gnu.gawk.bugs |
|---|---|
| Message-ID | <CABkLJUJ_+E-Wd0rLvq8msDh57JECBfaV4dgj9nJJdnfT5Gvf3w@mail.gmail.com> |
from the [email protected] email list 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. I was struck by a couple things while learning AWK programming last year: 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? 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. '{ if ( gsub( /pattern/, "replacement" ) ) { print } }' can give me the first. and '{ print gensub( /pattern/, "replacement", "g" ) }' can give me the second. If I want both, I'm stuck doing something dumb like '/pattern/ { print gensub( /pattern/, "replacement", "g" ) }' or '{ fixed = gensub( /pattern/, "replacement", "g" ) if ( fixed != $0 ) { print fixed } }' If I can do it with just sed's s command, I will. I have yet to run into a situation where I need to do something sed isn't good at and I also need \n references, but a new function that can do everything gensub() can do but that modifies the target variable in place and returns the number of replacements made would be nice. 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. When I was originally reading all this, I was thinking I wouldn't have a reason to write a function anyway. I was wrong. That function doesn't take any arguments and just references global variables, though. Not like sed supports user-defined functions to begin with. 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. [1] https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html [2] https://www.gnu.org/software/gawk/manual/html_node/Variable-Scope.html