Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Need Assistance
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 26th February 13:40
sdamian
External User
 
Posts: 1
Default Need Assistance



I am missing something here - something simple I'm sure. Here's my script.

It's not very sophisticated. Then again, I'm relatively new to awk.

date=$(date '+%Y.%m.%d')
time=$(date '+%H:%M')
mail_log="/var/spool/logs/log.$date"
y=$(awk ' {
t="'"$time"'"
if (/'$t'/ && $3 ~ /notification/ && $4 ~ /Deliver/) {
nmatches++ }
END { print nmatches }
}' $mail_log)

if (( $y >= 10 ))
then
$kill_isdelvd
fi

Here's the output I get . . .

awk: cmd. line:5: END { print nmatches }
awk: cmd. line:5: ^ parse error
  Reply With Quote


  sponsored links


2 26th February 13:40
bob harris
External User
 
Posts: 1
Default Need Assistance



The END has to be outside the first Pattern/Action set.

pattern { action }
pattern { action }
etc...

You start your script with an implied pattern (nothing so it matches
everything) and an action { t= ... END { ... } } with END inside of the
action. You need to put the END {...} outside of the action. END is
actually a pattern that matches the END of processing.

Also it is easier and cleaner to pass shell variables to awk via the -v
option.

date=$(date '+%Y.%m.%d')
time=$(date '+%H:%M')
mail_log="/var/spool/logs/log.$date"
y=$(awk -v t="$time" '
{
if ( $0 ~ t && $3 ~ /notification/ && $4 ~ /Deliver/) {
nmatches++
}
}
END { print nmatches } ' $mail_log)
if (( $y >= 10 ))
then
$kill_isdelvd
fi

---- Or here is another way to say what you are saying ----

date=$(date '+%Y.%m.%d')
time=$(date '+%H:%M')
mail_log="/var/spool/logs/log.$date"
y=$(awk -v t="$time" '
$0 ~ t && $3 ~ /notification/ && $4 ~ /Deliver/ {
nmatches++
}
END { print nmatches } ' $mail_log)
if (( $y >= 10 ))
then
$kill_isdelvd
fi

Notice in the 2nd alteration, the line selection is turned into the awk
pattern of the pattern/action pair, instead of doing this with an 'if'
in the action section.

Bob Harris
  Reply With Quote
3 26th February 13:40
demas
External User
 
Posts: 1
Default Need Assistance


y=$(awk ' {
t="'"$time"'"
if (($0 ~ t) && ($3 ~ "notification") && ($4 ~ "Deliver")) {
nmatches++ }}
END { print nmatches } ' $mail_log)

Your END section was inside curly braces

Note yow the curly braces have changed in my code.


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
  Reply With Quote
4 7th March 13:24
chris f.a. johnson
External User
 
Posts: 1
Default Need Assistance


In addition to the other advice you have received, you can save a
call to date:

eval `date "+date=+%Y.%m.%d time=%H:%M"`

--
Chris F.A. Johnson http://cfaj.freeshell.org
================================================== =================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
  Reply With Quote
5 7th March 13:24
sdamian
External User
 
Posts: 1
Default Need Assistance


Thank you, Bob. This not only worked, but helped me to better
understand where I went wrong. Incidentally, I went with the 2nd
alteration. It's cleaner.


-Damian
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666