scsh-users
[Top] [All Lists]

Re: awk in scsh

To: scsh@martigny.ai.mit.edu
Subject: Re: awk in scsh
From: Michel Schinz <schinz@studi.epfl.ch>
Date: 25 Jun 1996 09:10:33 +0200
Organization: Ecole Polytechnique Federale de Lausanne
jbell@capecod.net (Janet Bell) writes:

> I am a little confused at the exact specification of how awk works.
> It says that each matching clause is executed (unlike cond) and that
> each one must return a number of values equal to the number of state
> variables.

> Now, after you have parsed a line and are scanning the clauses, if
> you find one that matches, do you then execute it, rebind (or SET!)
> the state variables, and continue scanning clauses (so subsequent
> clauses see the new state)?  or do you go back to the top?

I think a small academic example will help here.  Consider the
following script:

(call-with-input-file "test"
  (lambda (in-port)
    (awk (read-line in-port) (ln) num ((a 0) (b 0))
         ("^[a-z]" => (lambda (match)
                        (format #t "match 1, line ~a, a=~a, b=~a~%" num a b)
                        (values 1 1)))

         ("[a-z]$" => (lambda (match)
                        (format #t "match 2, line ~a, a=~a, b=~a~%" num a b)
                        (values 2 2))))))

This script simply reads the file "test" line-by-line and print a
message when either the line starts (first regexp) or ends (second
regexp) with a lowercase letter; it also maintains two state
variables, a and b.  With the following file "test" as input:

a
a1
1a

the script produces (the two return values were omitted):

match 1, line 1, a=0, b=0
match 2, line 1, a=1, b=1
match 1, line 2, a=2, b=2
match 2, line 3, a=1, b=1

This should answer your question, since the first line ("a") matches
both regexps: the two clauses are executed in order and the second one
"sees" the modified version of the state variables (I admit that my
example is awfully complicated to answer such a small question, but
then it gives an example of awk's usage).

Michel.

<Prev in Thread] Current Thread [Next in Thread>