Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Help Please - awk works directly but not in a script
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 19th October 07:02
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script



Greetings,

I would like to deploy log monitoring in AIX, HP-UX, SUN Servers

# Saving the Last time stamp of the file
egrep "$(date +'%a %b %e %H:')" $ALOG |tail -1 >last_timestamp
awk -v T="$(<last_timestamp)" '/$T/,/ZZ/ {print $0}'< $ALOG

give me the following error:

awk: There is a regular expression error.
Invalid pattern.
The input line number is 1.
The source line number is 1.


If I do it directly, it works:

$cat last_timestamp
Wed Aug 30 15:36:19 2006

awk '/Wed Aug 30 15:33:36 2006/,/zz/ {print}'<$ALOG # It prints all
the info

Wed Aug 30 15:39:09 2006
Thread 1 advanced to log sequence 275604
Wed Aug 30 15:39:09 2006
Current log# 8 seq# 275604 mem# 0:
/u01/oradata/vrbhrpt/redolog_8a.ora
Wed Aug 30 15:39:09 2006
ARC0: Beginning to archive log# 1 seq# 275603
Current log# 8 seq# 275604 mem# 1:
/u02/oradata/vrbhrpt/redolog_8b.ora
Wed Aug 30 15:39:16 2006
ARC0: Completed archiving log# 1 seq# 275603


My goal is to run this script piping the above command to egrep (egrep
"ORA-") from cron every hour to catch errors errors (that start with
ORA-) and email/page.

I am saving the last timestamp, so that I can start with the next
second during my next run
so that I wont miss any lines in the log file for errors


Thank you for your help
BN
  Reply With Quote


 


2 19th October 07:02
jon labadie
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script



^ ^^
| Below you are using lower case z
It is not a shell variable, drop the $
  Reply With Quote
3 19th October 07:02
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


Thank you.

I have removed the "$" sign before T, Now its printing the entire file

awk -v T="$(<last_timestamp)" '/T/,/ZZ/ {print;}'< $ALOG

It now prints the entire file, not from the timestamp I wanted

awk '/Wed Aug 30 16:41:55 2006/,/ZZ/ {print}' <$ALOG

This is printing what I wanted, when I directly substitute the value
for T.

Regards & Thanks
BN
  Reply With Quote
4 19th October 07:02
gazelle
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


In article <1156971031.827623.174770@74g2000cwt.googlegroups. com>,
....


awk -v T="$(<last_timestamp)" "/$T/"',0 {print}' $ALOG

Although there are certainly better ways to do it that are more pure
AWK, more reliable, and more OT for this ng (c.l.a)
  Reply With Quote
5 19th October 07:02
janis
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


T is no shell variable ($T), nor a literal pattern value (/T/ or /$T/).
I have no awk at hand to check, but try this...

awk -v T="$(<last_timestamp)" 'T,/ZZ/'< $ALOG
Janis
  Reply With Quote
6 19th October 07:02
gazelle
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


Yeah, I suppose what you really want is:

awk "/$(<last_timestamp)/,0" $ALOG
  Reply With Quote
7 19th October 07:02
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


Greetings,

Thank you for the info.

why did you say

<<< Although there are certainly better ways to do it that are more pure
AWK, more reliable, and more OT for this ng (c.l.a) >>>>

I am not very good in awk. I am thinking that this is simple and easy.

I appreciate if you explain a little bit more

Regards & Thanks
BN
  Reply With Quote
8 19th October 07:02
ed morton
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


Do it this way:

awk -v T="$(<last_timestamp)" '$0~T,/ZZ/' < "$ALOG"

See question 24 in the comp.unix.shell FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#24) for how to use the
value of shell variables in awk scripts.

See the section on dynamic regexps in the GNU awk manual
(http://www.gnu.org/software/gawk/man...mputed-Regexps) for
how to use awk variables in regexp comparisons.

Ed.
  Reply With Quote
9 19th October 07:02
ed morton
External User
 
Posts: 1
Default Help Please - awk works directly but not in a script


I didn't even look at what you were trying to do in your shell script
leading up to the awk question. Rather than this mixture of shell
commands, pipes, and files:
egrep "$(date +'%a %b %e %H:')" $ALOG |tail -1 >last_timestamp
awk -v T="$(<last_timestamp)" '$0~T,/ZZ/' < "$ALOG"

you could just do it all in awk (in this case using GNU awk for
strftime() but you could pass that in as a var):

awk 'BEGIN{ date = strftime("%a %b %e %H:"); ARGV[ARGC++]=ARGV[1] }
NR == FNR { if ($0 ~ date) T = $0; next }
$0~T,/ZZ/' "$ALOG"

Regards,

Ed.
  Reply With Quote
Reply


Thread Tools
Display Modes




666