Forum Moderators: coopster & phranque

Message Too Old, No Replies

question about awk in perl

         

kachian

4:14 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



I wrote a script that monitor a file and i use awk to filter it

use File::Tail;
my $log= '/abc.txt';
my $file = File::Tail->new(name=>$log,tail=>1);
while(defined($_=$file->read))
{
chomp ($_);
{system("echo '$_' ¦ nawk -f lacawk");}
}

and this is the file lacawk

BEGIN{
cell = "";}
#name of alarm
/ALARM/{
alarm=$0;
next;}
/STATE/{
printf("%s",alarm) >> test
next;}

if i run the command tail -f abc.txt ¦ nawk -f lacawk
the result is very fine. but it's not when i use the perl script. can any one have an idea? and why?

kachian

4:18 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



sorry guys,, forgot!
by the command the printf is print the results in file test
but nothing come with the script

perl_diver

5:31 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



Try something other than sytem() which does not return program output back to perl.

use File::Tail;
my $log= '/abc.txt';
my $file = File::Tail->new(name=>$log,tail=>1);
while(defined($_=$file->read))
{
chomp ($_);
print qx/echo '$_' ¦ nawk -f lacawk/;
}

but you may need to supply more of a path to nawk:

print qx/echo '$_' ¦ usr/bin/nawk -f lacawk/;

kachian

7:44 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



oh! thanks for the help man. but it's not working even with the path /usr/bin/nawk, any one please help to work it out?

phranque

9:48 am on Mar 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], kachian!

have you tried just the print & echo without piping into nawk to make sure you are really reading (from the log file) what you think it should be?
then try simplifying your nawk program to just printf before you add any text processing.
one small step at a time, so to speak...

kachian

1:22 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



hi phranque , hi everyone.
first, i have tried that too, it's work fine.
the problem it's just abt the variables in awk .. this is my brief awk file
BEGIN{
cell = "";
state = "";
alarm = "";
}
# Clear alarms
/ [0-5][0-9] REPT:CELL/&&/ALARM SCANNING/{
cell = $3;
next;}
/A [0-5][0-9] REPT:CELL/&&/ALARM SCANNING/{
cell = $4;
next;}
# Critical alarms
/\*C[0-5][0-9] REPT:CELL/&&/ALARM SCANNING/{
cell = $3;
next;}
# Major alarms
/\*\*[0-5][0-9] REPT:CELL/&&/ALARM SCANNING/{
cell = $3;
next;}
# Status of alarms
/STATE: OFF NORMAL/{
state = "ON";
printf(" RCS %s %s %s\n",cell,state,alarm) >> "rcs_alrm";
next;}
/STATE: NORMAL/{
state = "OFF";
printf(" RCS %s %s %s\n",cell,state,alarm) >> "rcs_alrm";
next;}

sorry for the last lines ,, i didn't give in detail of my stiuation
my whole program is to read a logfile and filter the alarm of the hardware devices which is defined in few lines, and the name of the alarm and status of it in different line.
my point is the variable "cell" is a global variable, and it works in command: tail -f logfile ¦ nawk -f lacawk, it prints both the "cell" and "alarm".
but in perl script it understands and print only the variable "state" and leave the "cell" variable blank and i don't understand why is that? why it doesn't print "cell".