unconditional patterns into if/else 
Author Message
 unconditional patterns into if/else


> > How do I turn a set of patterns into IF/ELSE?
> > Right now, the script below works fine.

> > /DTYY$/    ||   \
> > /DTYK$/    ||   \
> > /DTPK/     ||   \
> > {print FILENAME "\t" $0}

> [...]

> #2 If you have a newer awk - nawk, mawk, gawk, tawk or HP-UX awk,
>    then you can combine and simplify those patterns:

>       /DT(Y(Y|K)$|PK)/

>    If you examine that closely you should be able to understand it.

I suspect the pattern /DTPK/ instead of /DTPK$/ was a typo, in which
case this


can be expressed like this


or like this


Just my $.02 worth...

Jim Monty

http://www.*-*-*.com/ ~monty/
Tempe, Arizona USA

Sun, 21 Oct 2001 03:00:00 GMT  
 unconditional patterns into if/else


>So I tried:

># comment
>if ( /DTYY$/    ||   \
>/DTYK$/    ||   \
>/DTPK/     ||   )

>{print FILENAME "\t" $0}

># end

>But I never got to the "else".
>When I tested this half, awk bailed out, at "if".
>So, what am I missing?

The if is an action thing and must be within the curly brace part,
not in the pattern part.

awk '{if( $0 ~ /DTYY$/ || $0 ~ /DTYK$/ || $0 ~ /DTPK/){
      print FILENAME "\t" $0} else {print "Error\t" $0}}' infile

but since you want to print something in either case, try using the ? :
construct (assuming your awk supports it):

gawk '{print ( ( $0 ~ /DTYY$/ || $0 ~ /DTYK$/ || $0 ~ /DTPK/) \
      ? FILENAME "\t" : "Error\t" ) $0}' infile

For this infile:


it produced this (as did the other script I wrote above):

infile  DTYK
Error   DTYKa
infile  DTYY
Error   DTYYa
infile  DTPK
infile  DTPKa
Error   DOTY

You could also tidy up the regular expression, as others have posted.

Chuck Demas
Needham, Mass.

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

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

Mon, 22 Oct 2001 03:00:00 GMT  
 unconditional patterns into if/else

>What I want to add is, if the patterns are NOT present, then output a message.

Assuming what you posted is your entire script,  you can achieve this
by changing the first action to:

    {print FILENAME "\t" $0; next}

and adding an unconditional action after it:

    { print "error message goes here" }

This gives you two actions:  one that gets executed on a match,  and one
that's executed when the match fails.  This is not exactly a
"general-purpose" practice, since you can only use it once per program,
but sometimes one is all you need  ;-)


Mon, 22 Oct 2001 03:00:00 GMT  
 [ 6 post ] 

 Relevant Pages 

1. Unconditional jump penalty on Pentium

2. for unconditional make

3. pattern,pattern problem

4. Pattern Definition GREP with Pattern Editing for MSDOS

5. How to write filesearch with begin-pattern till end-pattern

6. Troy, MI Patterns Group?

7. 2nd RFD: comp.software.patterns

8. OldStuff - Patterns

9. Forwarded message from patterns mailing list...

10. 2nd RFD: comp.software.patterns

11. Pattern to increment an instance variable

12. New Patterns in EducationCenter - where?


Powered by phpBB® Forum Software