Discussion Locked

awk - help with "|" as "or" and syntax

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have an awk statement on my log server that discards ignorable data and emails the rest once a day. The statement is

awk '! /\.debug\]/ && ! /\.info\]/ && ! ......

I want to combine the .debug] and .info] matches in one expression with or so I tried

awk '! /[\.debug\]|\.info\]]/ && ! ......

This doesn't seem to work. I was able to a simpler join as follows

&& ! /wally updater/ && ! /dilbert updater/ && ! ......

rewritten as

&& ! /[wally|dilbert] updater/ && ! ......

Any ideas why it isn't working with the first expression set?

8 replies

by gary_w

I'm a little rusty so bear with me...

You want all lines that don't contain ".debug]" or ".info]" right? What about this simple example:

awk '!/.debug]|.info]/ {print $0}' filename

Removing the bang ('!') prints all lines containing ".debug]" or ".info]"

This works on my machine anyway.

Gary

by gary_w

Hmmm. I am using a Sun box running SunOS 5.8. I tested with the korn shell.

Try again but use nawk. It seems to give a little more info in it's error messages.

Mine doesn't work if I escape the '.' and ']'.

Interesting.

by gary_w

Look close at your example:
awk '! /[\.debug\]|\.info\]]/ && ! ......
^ ^
| |
Try removing these square brackets.

Good thing it's a slow day today.
Gary

by gary_w

ARGH My little arrows don't line up. Try removing the square brackets that surround the expression. I believe all you need to surround the expression are the '/'s.

Ok, back to work.

by cpfeiffe

Thanks. I've printed your answers. I don't know when I'll get back to this, but I'm sure its a syntax error and if nothing else its just nice to see what works in another environment. I can play with it from here.

by cpfeiffe

Gary_W - how are you getting away with not escaping the . and ]? I did the exact same thing except that I did used a \ in front of the . and ] on both. I tried not escaping these and it didn't work either. What version of awk are you using and what OS are you on?

This discussion is closed to new replies.