Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mutually exclusive patterns

Status
Not open for further replies.

phife757

Programmer
Joined
Jul 16, 2008
Messages
2
Location
US
Hi all,

how do you match patterns in an "if-else" fashion?

for example, say I have a set of patterns to match:

/foo/ { print "foo" }
/bar/ { print "bar" }
/.*/ { print "default" }

so for input file:

foo
bar
raboof

I'm getting:

foo
default
bar
default
default

but i would like:

foo
bar
default


Notice the lines "foo" and "bar" match twice. I would like the last pattern to match, only of the other patterns do not match. Does anyone have a solution to this?

-Matt
 
Hi

Code:
/foo/ { print "foo"[red]; next[/red] }
/bar/ { print "bar"[red]; next[/red] }
/.*/  { print "default" }

[gray]# or[/gray]

/foo/ { print "foo" }
/bar/ { print "bar" }
[red]![/red]/[red](foo|bar)[/red]/  { print "default" }'

[gray]# or[/gray]

/foo/ { print "foo" }
/bar/ { print "bar" }
[red]![/red]/[red]foo[/red]/ [red]&&[/red] [red]![/red]/[red]bar[/red]/  { print "default" }

Feherke.
 
thanks, the first solution is perfect!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top