Computing.Net > Forums > Unix > Awk problem

Awk problem

Reply to Message Icon

Original Message
Name: vibhor_agarwalin
Date: February 1, 2005 at 02:05:43 Pacific
Subject: Awk problem
OS: Solaris
CPU/Ram: sparc
Comment:

Hello,

My awk script is something like:

BEGIN {print $var} /reg_exp/ END {print ...}

Now the first block after BEGIN does not print the value of variable.
But if i remove it from the scope of BEGIN like

BEGIN {} {print $var} /reg_exp/ END {print ...}
then it gets printed, which will make my script useless.

Do variable don't get printed in BEGIN block?

Also, one of my record in a file is too big for awk.
On processing that record my script terminates.
how can i overcome it.

Thanks in Advance

Vibhor Kumar Agarwal


Report Offensive Message For Removal


Response Number 1
Name: vgersh99
Date: February 1, 2005 at 06:41:57 Pacific
Reply: (edit)

how do you pass 'var' to awk?

if you pass it as:
nawk 'BEGIN {print $var} ...' var=someValue file

'var' is NOT available in the BEGIN block.

if you pass it as:
nawk -v var=someValue 'BEGIN {print $var} ...' file

'var' is avaible in BEGIN and everywhere else.

What version of awk are you using?

If on Solaris, try using 'nawk' instead of 'awk'.

Also if on Solaris and having problems with 'record too big', try using /usr/xpg4/bin/awk.

vlad
#include<disclaimer.h>


Report Offensive Follow Up For Removal

Response Number 2
Name: Jim Boothe
Date: February 1, 2005 at 13:19:15 Pacific
Reply: (edit)

Actually, if you pass var to awk, you would print that variable without a dollar sign, as you would any awk variable.  If instead you print $var , for that to be valid, var would need to be numeric, and it would print the field represented by the numerical value of var.  And of course this would print null within a BEGIN statement since no lines will have been read yet.


Report Offensive Follow Up For Removal

Response Number 3
Name: vibhor_agarwalin
Date: February 1, 2005 at 22:05:08 Pacific
Reply: (edit)

Sorry,
My apologies,

I mistakingly put a $ sign in front of the variables.

nawk and /usr/xpg4/awk solved my problems.
Thanks

I searched the manual pages of awk, i think it doesn't support -v.
What is /usr/xpg4/bin/awk
nawk is totally different from awk or a bit enhanced.

Vibhor Kumar Agarwal


Report Offensive Follow Up For Removal

Response Number 4
Name: vibhor_agarwalin
Date: February 1, 2005 at 22:06:34 Pacific
Reply: (edit)

Ooops,

I missed something.

How can i find the version of a command.
You are asking about awk

Vibhor Kumar Agarwal


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Awk problem

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge