Substituting variable value in AWK /start/,/stop/

Hi all u brilient people on the forum...
I am trying to call the variable value in awk command for search pattern /start/,/stop/ but i am nt able to do this ....
wat i did is ..i have created two variable YESTERDAY and TODAY and passed the y'day n 2'days dates in it...like this

YESTERDAY=`TZ=aaa24 date +"%b %d"`
TODAY=`date +"%b %d"`

now am trying call this variable value in awk command which will search the content of the file between Y'days and 2days date and redirect the output to another file ....As this

awk '/"$YESTERDAY {print}"/,/"$TODAY {print}"/' $HOME/alert_out.log > $HOME/alert_work.log

bt its not working.....:confused:
Can anybody help me out How can i acheive this??
Thanks in advance...:slight_smile:

to avoid the troublesome quotes, use -v option of awk to pass in variables from the shell.

what is your pattern??
is it $YESTERDAY,$TODAY ?
you can directly use double quotes instead the single ones in the whole command.

10x...4 reply to my thread bt
How can i use -v option in awk for /start/,/stop/ search option...

Kanchan
Also my search pattern is the date which will retrieve by the variable which i was defined above....
It has to search the result based on the Yesterdays and todays date.

I was already tried to put in double qoute bt no luck...

a="pattern1"
awk -v var=$a '$0~var,/pattern2/' file

10x 4 reply...
bt still its not working..
I think u ddnt undersand my requirement.

Here i want to retrieve the value based on the YESTERDAYS Date and TODAYS date and my script will redirect the content of the file Between yesterdays date and todays date. The file on which i am taking the search is having the daily update mentioning the date format e.g say Apr 16 ,Apr 15,Apr 14 like that. so if i pass the value Apr 15 in place of YESTERDAY variable and Apr 16 for TODAYS then it has to give the content of the file between these two dates.

i am using awk command of search string '/START/,/STOP/' where START if 4 YESTERDAY and STOP 4 TODAY... and the syntax which awk command use for this kind of search is shown above in colored..
So here i have already defined the variable YESTERDAY and TODAY...
My problem is how to pass the value for both the variable.....

My code is some what like this

YESTERDAY=`TZ=aaa24 date +"%b %d"`
TODAY=`date +"%b %d"`
echo $TODAY
echo $YESTERDAY
awk '/"$YESTERDAY {print}"/,/"$TODAY {print}"/' $HOME/alert_out.log >> $HOME/alert_work.log
 
but here its not passing the value 4 both variables though its still creating the file $HOME/alert_work.log but with zeor content.

the syntax for your awk statement is wrong. I have already showed you how you can pass variables to awk from shell using an example.

Hi Ghostdog

And that pattern2 as well i want to call trough varaible thn how can i do this???

look at how pattern1 is used. its the same.

Hi ghostdog

a=pattern1
b=pattern2
awk -v var=$a,var1=$b '$0~var,$0~var1' file

Here what i used ......
can you correct me...... Its not working

awk -v var=$a -v var1=$b '$0~var,$0~var1' file

next time, don't just say "it's not working". show how its not working by provided error messages if any, your input file samples etc.

Hi ghostdog

-------here is my code which i have written (for bottleneck look at the code RED)------
#!/bin/ksh
for SID in `cat $HOME/appleora/dblist`
do
cd $HOME/appleora/$SID/admin/bdump
cat alert_${SID}.log >>$HOME/alert_${SID}.log
cd
if [ -f alert_${SID}.log ]
then
mv alert_${SID}.log alert_out.log
touch alert_${SID}.log
YESTERDAY=`TZ=aaa24 date +"%b %d"`
TODAY=`date +"%b %d"`
echo $TODAY
echo $YESTERDAY
#awk '/Apr 15/,/Apr 16/' $HOME/alert_out.log >> $HOME/alert_work.log
#awk '/$YESTERDAY/,/$TODAY/' $HOME/alert_out.log > $HOME/alert_work.log
awk -v var=$YESTERDAY,var1=$TODAY '$0~var,$0~var1' $HOME/alert_out.log > $HOME/alert_work.log
cat alert_work.log >> alert_${SID}.hist
grep ORA- alert_work.log >> alert.err
fi
for DBA in `cat $HOME/appleora/dbalist`
do
if [ `cat alert.err|wc -l` -gt 0 ]
then
mailx -s "${SID} ORACLE ALERT ERRORS" $DBA < alert.err
else
mailx -s "${SID} ORACHECK RUN SUCCESFULLY NO ERROR" $DBA
fi
done
rm -f alert.err
rm -f alert_work.log
done
 
Now 
-----Here is my $HOME/alert_out.log file on which i am doing search and i am intreested in content between Apr 15 and Apr 16----
Wed Apr 15 09:37:05 2009
Thread 1 advanced to log sequence 46
  Current log# 3 seq# 46 mem# 0: C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO03.LOG
Wed Apr 15 22:00:14 2009
Thread 1 advanced to log sequence 47
  Current log# 1 seq# 47 mem# 0: C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO01.LOG
b_recovery_file_dest_size of 2048 MB is 0.00% used. This is a
user-specified limit on the amount of space that will be used by this
database for recovery-related files, and does not reflect the amount of
space available in the underlying filesystem or ASM diskgroup
Errors in file c:\oracle\product\10.2.0\admin\orcl\udump\orcl_ora_5388.trc:
ORA-00600: internal error code, arguments: [LibraryCacheNotEmptyOnClose], [], [], [], [], [], [], []
Thu Apr 16 09:30:56 2009

that means o/p should be in green
=====================================
Now if i use 
#awk '/Apr 15/,/Apr 16/' $HOME/alert_out.log >> $HOME/alert_work.log
then i am getting desired output as well as i am getting mail thru mailx command 
=====================================
=====================================
if i use 
#awk '/$YESTERDAY/,/$TODAY/' $HOME/alert_out.log > $HOME/alert_work.log
then this file is get created > $HOME/alert_work.log this file bt with no content 
=====================================
=====================================
and if i use 
awk -v var=$YESTERDAY,var1=$TODAY '$0~var,$0~var1' $HOME/alert_out.log > $HOME/alert_work.log
as u suggested......
i am getting below message
sh oracheck.sh
Apr 16
Apr 15
awk: syntax error near line 1
awk: bailing out near line 1
=====================================

Now can u help me out......

awk -v var="$YESTERDAY" -v var1="$TODAY" '$0~var,$0~var1' $HOME/alert_out.log > $HOME/alert_work.log

[*] running out of spoons....

Hi vgersh99
I have tried with, as u have suggestd bt its giving me below error..........

Apr 16
Apr 15
oracheck.sh: Apr 15: not found
oracheck.sh: Apr 16: not found
awk: syntax error near line 1
awk: bailing out near line 1
awk -v var="$YESTERDAY" -v var1="$TODAY" '$0~var,$0~var1' $HOME/alert_out.log > $HOME/alert_work.log
awk -v var=`"$YESTERDAY"` -v var1=`"$TODAY"` '/$0~var/,/$0~var1/' $HOME/alert_out.log > $HOME/alert_work.log

I have tried out with both this optionss.....
but in both the cases its giving me same error

the ' oracheck.sh ' errors have nothing to do with the question you're asking originally - check the $PATH .
As far as 'awk' errors are concerned - as always, if you're on Solaris, use either /usr/bin/nawk or /usr/xpg4/bin/aw k instead of plain old '/usr/bin/awk '.
Good luck.