[comp.unix.i386] Awk with reg. exp. question

palowoda@fiver.UUCP (Bob Palowoda) (03/26/90)

  I'm have a problem understanding regular expression usage in 'awk'.
Maybe someone can help me out here. I might be useing the wrong awk,
I have tried nawk, xenix's awk, oawk, and gnu awk version 1.7.

Essentially the below script is a mail file field parser. 
What I don't understand is the expressions with '\t' in them.
I look in all kinds of references.    

Included is a scirpt of the error, and what I thing the line
is to be parsed. (For the FIELD) var.

---------------------------------------
This would be the line to parse, it is converted to upper case by
ucasep.
From: palowoda@fiver.UUCP (Bob Palowoda)
--------------------------------------------------------------
# This assumes that the field-names are all upper case
for f in $*
do
        FIELD=$FIELD"|"`echo $f | ucasep`
done

${awk} "
/^($FIELD)[ \t]*:/"' {
        if (current != "") print current;
        current = $0;
        next;
}

/^[^ \t:]+[ \t]*:/ {                        <-------- Here is where I think
        if (current != "") print current;             the problem is
        current = "";
}

/^$/ {
        if (current != "") print current;
        current = "";
        exit;
}

/^[ \t]/ {
        if (current != "")
                current = current " " $0;
}

END {
        if (current != "") print current;
}
--------------------------------------
And now for the script of the error.

awk: illegal primary in regular expression ^(|REPLY-TO|FROM|SENDER)[ \t]*: at REPLY-TO|FROM|SENDER)[ \t]*:
 source line number 2
 context is
        /^(|REPLY-TO|FROM|SENDER)[ >>>  \t]*:/ <<<  {
                                         ^
                                         |
                                         Is this the error?
-----------------------------------------------------------------

---Bob

dcm@moria.UUCP (David C. Miller) (03/28/90)

>And now for the script of the error.
>
>awk: illegal primary in regular expression ^(|REPLY-TO|FROM|SENDER)[ \t]*: at REPLY-TO|FROM|SENDER)[ \t]*:
> source line number 2
> context is
>        /^(|REPLY-TO|FROM|SENDER)[ >>>  \t]*:/ <<<  {
>                                         ^
>                                         |
>                                         Is this the error?


No, awk got seriously confused by the start of the statement and flagged
the spot where it finally realized it was lost.  The pattern being
used is the first one, /^($FIELD)[ \t]*:/.

So the problem is with the arguments that this program is being called
with, in particular, one that reads "|reply-to|from|sender".  The case
of that may vary.  That argument should read "reply-to|from|sender".

Note the absence of the leading vertical bar.


>-----------------------------------------------------------------
>
>---Bob

---David
...!att!tsdiag!ka2qhd!moria!dcm