Newsgroups: comp.os.os2.advocacy
From: panos@db.toronto.edu (Panos Economopoulos)
Subject: Re: What REXX does
Organization: Department of Computer Science, University of Toronto
Date: 9 Sep 92 14:58:18 GMT

WILLS@LSTC2VM.stortek.com (Cheyenne Wills) writes:

>A prime example is editor macros.  KEDIT from Mansfield and EPM
>(included with OS/2) both use Rexx as a macro language (I use KEDIT
>and only know that EPM supports Rexx, plus there are probably other
>editors out there that also use Rexx).
> 
>Anyway... a simple KEDIT macro to count the number of nonblank lines
>is a file that is being edited follows:
> 
>----[blankcnt.kex]-----------
>/* Count blank lines */
>nonblank = 0
>blank = 0
>'Top'
>'Next'
>Do While rc = 0
>    'Extract/curline/'
>    If Space( curline.3 ) <> '' Then nonblank = nonblank + 1
>    Else blank = blank + 1
>    'Next'
>End
>'Top'
>'Msg There are' nonblank 'nonblank lines and' blank 'blank lines in the file'
>-----------------------------
> 
>The statements in quotes (Top, Next, Extract and Msg) are all
>KEDIT commands that are accessed via the macro interface.  When KEDIT
>sees the command "blankcnt" on its command line it will search
>its own list of internal commands and upon not finding it will search
>for a file named "blankcnt.kex".  It will invoke the Rexx interpreter
>pointing to the "blankcnt.kex" file.  In addition to passing the
>name of the Rexx program, KEDIT will inform the interpreter that the
>default command environment is KEDIT's.  What this means is that
>any statement that Rexx doesn't recognize as a valid Rexx statement
>will be passed to that command environment (a .CMD file the default
>command environement is CMD.EXE's).
> 
>The variable curline.3 is set by the EXTRACT command (there are
>ways of a program to set and retrieve Rexx variables).  In this case
>curline.3 contains the contents of the current line.
> 

Just for the purpose of showing how the above can be accomplished
in a UNIX environment and without intending to pass any judgement
regarding the merits of either environment, one can use the following
AWK program to count the nonblank lines of a file:

!/^$/  {count++}
END    {print count}

/pattern/  matches a line that contains <<pattern>> and performs
the actions between the curly brackets for that line.  ! is the boolean NOT. 
!/pattern/ matches a line that does NOT contain <<pattern>> and performs
the action between the curly brackets for that line. 
/^/ matches the beginning of the line (just before the first character).
/^abcz/  matches a line that starts with <<abcz>>. /$/ matches the end
of a line (i.e. just after the last character). So, /^hello$/ matches
a line that is exactly <<hello>>
/^$/ matches the line that has no characters (it makes sense if you think
carefully what the ^ and $ match)

So,   ! /^$/   will matches lines that are not lines with no characters.
For these lines a counter will be incremented (variables are initialized
to 0, by default).

END is a special pattern that is activated after all input has been 
processed. The only think we do there is just output the count.

BTW, another way to say the same thing would be to use the pattern
/.*/ instead of  !/^$/ which basically means:
match a line that has one character -any character- repeated 0 or more times.

In an editor like vi, one could issue the above command, either ad hoc

:!awk '!/^$/ {count++} END {print count}' filename

where filename could be % for the file that is currently beingedited
or one could map a key to the above command line.
If the shell used has a history mechanism (csh/tcsh etc.) the ! which
is going to be interpreted by the shell should be escaped, that is

:!awk '\!/^$/ {count++......

-----------------------------------------------------------------------
Panos Economopoulos --         panos@csri.toronto.edu      (ARPA/CSNet)
University of Toronto          {uunet, ...}!utcsri!panos   (UUCP)
                               panos@csri.utoronto	   (Bitnet)
                               panos@csri.toronto.cdn      (CDNnet/EAN/X.400)