regex: how to limit pattern match to first occurance 
Author Message
 regex: how to limit pattern match to first occurance

In gawk (3.0.3) I have:

/^From: / { from = $2 }    
/^Subject: / { subj = $2 }      

How can I limit this pattern match to ONLY the first occurance
meaning the first line encountered? I am parsing smtp headers and
do not want subsequent matches.

I looked at the Sed & Awk | Regex (owl) books and maybe it's in
there but I am not getting it..somehow.

TIA
~kevin



Sun, 18 Mar 2001 03:00:00 GMT  
 regex: how to limit pattern match to first occurance
Either:

/^From: /    { if ( from == "" ) from = $2 }    
/^Subject: / { if ( subj == "" ) subj = $2 }      

Or:

/^From: /    && from == "" { from = $2 }    
/^Subject: / && subj == "" { subj = $2 }      

will do the trick for you.

--
Best regards,
 _ __                      _    ,   _ _ _
' )  )     /         _/_  ' )  /   ' ) ) )
 /--' ____/___/> __  /     /--/     / / / __,_  __  o _   ______
/  \_(_) /_) (__/ (_<__   /  ( o   / ' (_(_) (_/ (_<_/_)_(_) / <_

Robert H. Morrison                      Tel:   +49 721 9628 167
Software Development, Basis Team        FAX:   +49 721 9628 149


Quote:
> In gawk (3.0.3) I have:

> /^From: / { from = $2 }    
> /^Subject: / { subj = $2 }      

> How can I limit this pattern match to ONLY the first occurance
> meaning the first line encountered? I am parsing smtp headers and
> do not want subsequent matches.

> I looked at the Sed & Awk | Regex (owl) books and maybe it's in
> there but I am not getting it..somehow.

> TIA
> ~kevin



Sun, 18 Mar 2001 03:00:00 GMT  
 regex: how to limit pattern match to first occurance


Quote:
>In gawk (3.0.3) I have:

>/^From: / { from = $2 }    
>/^Subject: / { subj = $2 }      

>How can I limit this pattern match to ONLY the first occurance
>meaning the first line encountered? I am parsing smtp headers and
>do not want subsequent matches.

You could set a flag after finding the match, something like this:

/^From: /    && flag_f==0 { from = $2; flag_f=1 }    
/^Subject: / && flag_s==0 { subj = $2; flag_s=1 }      

flag_f and flag_s start as 0, and then get set when the first line
is found.

This could be written more compactly as:

/^From: /    && f++==0 { from = $2 }    
/^Subject: / && s++==0 { subj = $2 }      

or

/^From: /    && !f++ { from = $2 }    
/^Subject: / && !s++ { subj = $2 }      

Chuck Demas
Needham, Mass.

Quote:

>I looked at the Sed & Awk | Regex (owl) books and maybe it's in
>there but I am not getting it..somehow.

>TIA
>~kevin

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 18 Mar 2001 03:00:00 GMT  
 regex: how to limit pattern match to first occurance

Quote:
> /^From: / { from = $2 }
> /^Subject: / { subj = $2 }

> How can I limit this pattern match to ONLY the first occurance
> meaning the first line encountered?

($0~/^From: /)&&(fromFlag==0) { from = $2 ; fromFlag=1}
($0~/^Subject: /)&&(subjFlag==0) { subj = $2; subjFlag=1 }


Mon, 19 Mar 2001 03:00:00 GMT  
 regex: how to limit pattern match to first occurance
You can use a flag;

BEGIN                       {flag=0}
...

/^From: /&&flag!=1 { from = $2;flag=1 }
/^Subject: /                 { subj = $2;flag=0 }

Anatoly.

Quote:

> In gawk (3.0.3) I have:

> /^From: / { from = $2 }
> /^Subject: / { subj = $2 }

> How can I limit this pattern match to ONLY the first occurance
> meaning the first line encountered? I am parsing smtp headers and
> do not want subsequent matches.

> I looked at the Sed & Awk | Regex (owl) books and maybe it's in
> there but I am not getting it..somehow.

> TIA
> ~kevin

--


:-) 4MD.AP10, 100 Abbott Pk Telephone: +1 (847) 935-4660         )-:
:-) Abbott Park, IL 60064   Fax: +1 (847) 935-0548               )-:


Mon, 26 Mar 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Expect Matching patterns to find the first occurance

2. My first regex.error: match failure

3. IMAQ pattern matching template size limit

4. Pattern-Matching : who's on first?

5. Finding the first of occurance of an element in an array

6. iss-matching - the free Regular Expression / Pattern Matching cluster

7. Problems limiting expect pattern match to within specific text - greedy (.*) in regex is killing me!

8. Store regex match in a variable

9. regex matching and text moving

10. Regex \w does not match underscore

11. RegEx ASCII character matching

12. Need regex help (or bug in match)

 

 
Powered by phpBB® Forum Software