I can't seem to pass variables properly into a nawk statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I can't seem to pass variables properly into a nawk statement
# 1  
Old 03-30-2008
I can't seem to pass variables properly into a nawk statement

Ok, So up front I'm going to say that I'm a very elementary scripter, and I tend to use tools I don't fully understand, but I shotgun at something until I can get it to work...that said, I can't for the life of me understand why I can't get this to go down the way I want it to.

The goal:
-to establish todays date
-to take a look at a log file (in this case the /var/adm/messages log)
-to check if the last line of the log matches todays date and if so ... (eventually it will be to dump all lines of the log with todays date to a text file that will at some point be picked up by the notification server and go out in an email)

My issue is that though I can establish the date, and create variables based upon it, I can't get nawk to accept the variables I've created.

What I have so far:

Code:
#!/usr/bin/bash
#collect todays date (date)
d=`date|awk '{print $3}'`
#collect todays date (month)
m=`date|awk '{print $2}'`
#collect errors for todays date (day and month)
e=`grep ""$d" "$m"" /var/adm/messages`
tail -1 /var/adm/messages|nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'
#if 
#        [ "$z" = "success" ]; 
#then echo "$e"
#fi

this returns nothing, no errors, just goes straight back to the prompt.

Now as a matter of checking my work I've tried replacing that last nawk statement with
Code:
tail -1 /var/adm/messages|nawk -v m=$m  '{print $m}'

I figured this would at least tell me if my variable is making it into the nawk statement, but this returns:

"nawk: illegal field $(Mar)
input record number 1
source line number 1"

the last thing that I've tried, just to make sure my syntax isn't completely foobar was
Code:
tail -1 /var/adm/messages|nawk -v m=$m -v d=$d  '{if ($1 == "Mar" && $2 == "30") print "success"}'

and that returns success...

So from what I can gather, my overall nawk statement is good, it's just not using the variables from the rest of the script.

So any input would be appreciated. Also (although, I want to learn this way) if anyone has any insight on a better method of keeping track of the messages log (or any log) please chime in.
# 2  
Old 03-30-2008
In awk, $m means the field number indicated by the number in the variable m. So no wonder that part didn't work.

Something tells me the values of your variables are not completely right. We saw that m equals "Mar" but what about n? I would say that what you have ought to work but I haven't looked that closely.

As an aside, date has options to output just the month name or the day within the month; see its manual page for details. That's also more portable because the default date output format depends on the user's locale, etc.

You are not using e for anything. I would advise against using backticks -- just redirect the output straight where you want it. But the nawk script looks like a promising start.

P.S. The log file is probably growing while you work so could it be that you got unlucky, and the last line didn't happen to have today's date when you tested that awk script?

Last edited by era; 03-30-2008 at 06:55 AM.. Reason: Add P.S.
# 3  
Old 03-30-2008
Thanks for the input. I'm not sure what you mean by

"In awk, $m means the field number indicated by the number in the variable m. So no wonder that part didn't work."

I've checked and rechecked my variables and I'm relatively certain that they are correct. I know date ...print $3 gives me the day (for today the number 30) and I know date... print $2 gives me the Month (for today Mar). So for today for example, I know $d is 30 and $m is Mar.

So for example the last line of the /var/adm/messages currently is:

Mar 30 06:20:29 bms02-XXX-QN-XXXXX-XXXX inetd[164]: [ID 101010 daemon.error] ftp/tcp: bind: Address already in use

so by my understanding when I invoke
Code:
nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'
#if

I'm saying
-within this statement make m the value of $m outside the statement
-within this statement make d the value of $d outside the statement
-if the first column of the last line of the log is equal to $m (Mar)
and
-the second column of the last line of the log is equal to $d (30)
then
-print "success"

but from what I'm gathering within the nawk statement, my variables aren't being set, so i'm basically getting

if column 1 = ""
and
if column 2 = ""
print "success"

but since that's not what column 1 & 2 are, it's failing and printing nothing

I hope that I'm explaining myself well here...I'm new to this forum, and I don't want to look silly...

Regarding $e, that's for future use...if I can get the nawk to print success, then once I uncomment those last few lines the bash if statement should make it go back through the log and print all the messages for that day (which as I said will eventually get dumped into an email so we can all panic over failed root logins)

Oh and yes the log is growing, but the last line always starts with Mar 30, so no matter what the line is, I should still be able to chop that off.

...Sorry for being so long winded, it's just my style
# 4  
Old 03-30-2008
No, $d means $30 and $m means $Mar but you want 30 and Mar without the dollar signs.
# 5  
Old 03-30-2008
Hi,

Given that AWK has a special way of passing variables into it, "$m" and "$d" are not recognized within AWK, it knows only m ( m="$m" ) and d ( d="$d" ).

Code:
nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'     # incorrect use of passing variables

So the correct use is:
Code:
nawk -v m=$m -v d=$d  '{if ($1 == m && $2 == d) print "success"}'

# 6  
Old 03-30-2008
Quote:
Originally Posted by rubin
Hi,

Given that AWK has a special way of passing variables into it, "$m" and "$d" are not recognized within AWK, it knows only m ( m="$m" ) and d ( d="$d" ).

Code:
nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'     # incorrect use of passing variables

So the correct use is:
Code:
nawk -v m=$m -v d=$d  '{if ($1 == m && $2 == d) print "success"}'

That was it. I KNEW it was going to come down to me not understanding the syntax correctly. Thank you so much for your succinct answer.

era, you got there first, and rubin, you made it all clear. Again, thank you.

and once I uncommented (and remembered to add ;then) to my if statement, I got the log dump I was looking for.

Just out of curiosity, did I choose a completely convoluted solution to my issue (dumping only todays log) or was this at least remotely a intelligent method?
# 7  
Old 03-30-2008
It's not convoluted at all, and you were very close already on the first try. Also, compliments for your ideas for how to debug the problem; again, you were so close, and it was just sheer bad luck you didn't figure it out completely on your own.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to pass shell arguments into Nawk/awk

Hi, I am in critical need of help, Thanks a ton for your help. I need to know how to pass the shell argument into nawk code in AIX. so that my file gets passed into that awk script and it can execute it part. To be detail, i have more than 100 files and in those files a particular field... (6 Replies)
Discussion started by: Selva_2507
6 Replies

2. Shell Programming and Scripting

How to pass nawk variable to shell within the same script?

Hi All, I tried googling but so far no luck, can someone tell me how pass the variable value used inside the nawk command to shell. In the below script i get the value of $c (without color: Total Executed: " c ") but the printf which is outside the nawk command doesn't print the value or it... (4 Replies)
Discussion started by: Optimus81
4 Replies

3. Shell Programming and Scripting

Bash-how to properly READ and PASTE variables.

Recently i made a script for a project at molecular dynamics but am stuck at the last step.The thing i want to do is to ask the user to input the number of particles, then replace the bolded numbers at lines 9 and 17.. code #!/bin/bash #read number of particles echo "insert the number of... (2 Replies)
Discussion started by: arisinhell
2 Replies

4. Shell Programming and Scripting

Pass parameter to nawk from shell script

I need to parse log files using nawk, but I'm not able to pass script input argument (date) to nawk, for example: ------------ #!/bin/ksh read date nawk -F, '{if($1==date) print $4" "$5}' ------------- Is there a way to pass an argument to nawk from shell script. Many thanks... (8 Replies)
Discussion started by: samer.odeh
8 Replies

5. Shell Programming and Scripting

Can't get shell parameters to pass properly to sqlplus

Gurus, The issue I'm having is that my Shell won't accept SQL parameters properly...... Here's they way I'm running it.... applmgr@ga006hds => sh CW_MigrationDeployScript.sh apps <appspwd> <SID> '01-JAN' '31-MAR' The process just hangs not submitting the SQL job... ... (3 Replies)
Discussion started by: WhoDatWhoDer
3 Replies

6. AIX

can't parse this nawk statement

hi all i have the following portion in an xml file: </n:AOMessage> <?xml version="1.0" encoding="UTF-8"?> <n:AOMessage xmlns:n="urn:ao:hs:update:shell" xmlns:bo="urn:ao:hs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ao:hs:update:shell... (0 Replies)
Discussion started by: chipahoys
0 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Shell Programming and Scripting

Shell script not processing if statement properly

Hi I am trying to create a shell script that will look for a contracthead file first and if the contract head file does not exist on day1 exit script. Now on day2 if contracthead exists or not run the script uploading files in order such as contract line then contract contact so the... (2 Replies)
Discussion started by: jonathan184
2 Replies

9. Shell Programming and Scripting

is it possible to pass external variable values to nawk?

Dear friends, please tell me how to pass the external variable values to the nawk command. length=`expr $len2 - $len1` i need to pass $length to following nawk command as mentioned below. nawk '{if((x=index($0,"W/X"))>0){id=substr($0,x, $length);print x;print id;}}' filename1 but I am... (1 Reply)
Discussion started by: swamymns
1 Replies

10. UNIX for Advanced & Expert Users

pf not working properly even with only "pass in all" and "pass out all" rules

i have two rules in my pf.conf file, "pass in all" and "pass out all" i was having issues with getting pf working to begin with, so i went with starting from nothing and working on up. i have an ultrasparc ultra1 200e, with an added 4-port fast ethernet sbus card, running "3.4 GENERIC#85... (4 Replies)
Discussion started by: xyyz
4 Replies
Login or Register to Ask a Question