EmbedUX
September 30, 2009, 6:43am
1
Hi,
I am accepting a string from user. compare this output with the awk output as below...
echo "\n\n\tDay [01,02....]: \c"
read day
awk '{ if($day == $2) { if ($mon == $1) { print "Yes" }}}' syslog.txt
I am getting the follwoing error
awk: Field $() is not correct.
The input line number is 1. The file is syslog.txt.
The source line number is 1.
Please suggest the changes and help on using nested if in awk.....
Thanks
Something like this:
read day
read month
awk -v d=$day -v m=$month '$2==d && $1==m {print "Yes"}' syslog.txt
EmbedUX
September 30, 2009, 7:07am
3
Thanks for the suggestion...but it is not giving any output... mu code is somthing like this
echo "\n\n\tDay [01,02....]: \c"
read day
echo "\n\n\tMonth [Jan, Feb....]: \c"
read mon
cat /var/adm/syslog/syslog.log > syslog.txt
awk -v d=$day -v m=$month '($2==d && $1==m) {print "Yes"}' syslog.txt
syslog.txt is a redirected file which has following output.
Sep 21 08:54:07 powaihp syslogd: restart
Sep 21 08:54:07 powaihp vmunix:
.
.
I wish to extract first column and compare it with the user's input (mon)and also compare the day with the user's input (day)...... when both match display some message.....
Kindly help.....
The name of the shell variable you used (mon) don't match with the name that you assign to the awk variable (month), it should be:
echo "\n\n\tDay [01,02....]: \c"
read day
echo "\n\n\tMonth [Jan, Feb....]: \c"
read month
cat /var/adm/syslog/syslog.log > syslog.txt
awk -v d=$day -v m=$month '($2==d && $1==m) {print "Yes"}' syslog.txt
Great.......this is working...
Thank you very much for the suggetion and correction....
---------- Post updated 10-01-09 at 02:48 PM ---------- Previous update was 09-30-09 at 05:27 PM ----------
I am finding it difficult to print else part of the if condition above..
Can somebody help me with syntax for
awk -v d=$day -v m=$month '($2==d && $1==m) {print "Yes"}' syslog.txt
As the above command prints yes when the condition is met, it must print No when the condition fails....
Thanks in advance...
Something like this:
awk -v d=$day -v m=$month '
if( $2==d && $1==m ){
print "Yes"
}
else {
print "False"
}' syslog.txt
Or just:
awk -v d=$day -v m=$month '($2==d && $1==m){print "Yes";next}{print "False"}' syslog.txt