Steve Litt via plug on 10 Sep 2020 08:27:31 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] Awk *language*: was Win Subsystem for Linux2 broken


On Thu, 10 Sep 2020 10:04:49 -0400
Walt Mankowski via plug <plug@lists.phillylinux.org> wrote:

> > Awk Command
> > https://linoxide.com/linux-command/awk-command-in-linux/  
> 
> The awk "command"? It's a language!
> 
> Walt

This is a ubiquitous and perpetual problem. Web-search for any kind of
help on awk, and 90% of the replies do the whole thing as a one-liner.
Code that should be five substantial lines of awk are treated as
arguments. People discussing awk seem to judge their awk-worthiness by
how complex their one-liners can get.

Awk is a beautiful, beautiful language for stream processing when
there's not too much state involved. The implied outer-loop makes
programs a level simpler. Awk's been around forever, long ago debugged
and optimized. It's on every POSIX computer I've ever seen, although
not necessarily quite the same version and feature set.

The following is my awk program for printing every line of a file
until, but not including, a line containing ARGV[1]:

=============================================================
#!/usr/bin/awk -We

# PUBLIC DOMAIN, NO WARRANTY

function usage(args){
        printf "Error: ARGC was %d, should have been 2.\n\n", args
        print "USAGE: cat file | upuntil.awk regex_start_ignoring_here\n" 
	print "or upuntil.awk regex_start_ignoring_here < file\n"
	exit 1
        }
BEGIN{
        if(ARGC != 2){
                usage(ARGC)
        }
        found=0
        upuntil = ARGV[1]
        ARGC = 0
        }

$0 ~  upuntil {
        found = 1
}
found != 1 {
        print $0
}
=============================================================

I have no doubt there are folks who can turn the preceding into a
one-liner, perhaps even a one liner less than 80 columns. But it will
be readable only to 1liner afficianados, it won't be easily reuseable,
and it won't include argument error checking.

Break logic is pretty easy in awk, making it a good reporting language.
Who hasn't faced the "tealeaves" problem, in which the totals must
print *above* the line items. You have to read the tealeaves to predict
the total and count of the line items.

Or not. On starting a new group, write the old group's totals on lines
that are prepended with a properly long, zero filled line number,
followed by the letter A. Then write each line item of the new group
prepended with the same zero filled line number, but followed by the
letter E. Pipe it through sort and group totals sort before the group.

There's no end to what can be done with awk if it's not restricted to
being a 1 liner.

SteveT

Steve Litt 
Autumn 2020 featured book: Thriving in Tough Times
http://www.troubleshooters.com/thrive
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug