Awk and Variables

Hi,
I have some files in metrica (assume a pre-defined format) which i need to process and have some values in a .csv file. The script that does this, part of which is:

procMetricaOMData()
{
host=$1
fileIN=$2
fileOUT=$3

    $CATCMD $fileIN | \\
    awk -f scripts/OMParser.awk -v host=$host >> $fileOUT

}

.....
host=`hostname`
procMetricaOMData $host $fileIN output/om/$om-$app.csv

The Awk script goes like this:

BEGIN {FS=" "; printf("%s,", host)}
$1 !~ /^#/ && $1 !~ /{/ && $1 !~ /}/ && $2 !~ /{/ {
if ($1 == "start") { printf("%s:%s,", substr($2,1,2),substr($2,3,2)) }
else if ($1 == "end") { printf("%s:%s,", substr($2,1,2),substr($2,3,2)) }
else { printf("%s,",$2) }
}
END {printf("\n")}

The expected output should be something like:

HOST,WEEK,WEEKDAY,day,node,HOUR,start,end,SAMPLING INTERVAL,TotalRequest,MSRequest,NWIRequest,SMSBrequest,SMSBack,SMSBNack,MSRequestSuccess,NWIRequestSuccess,SMSCCongestion,ApplicationCongestion,InvalidParameters,MSIllegalSubscriber,MWIllegalSubscriber,ApplicationUnavailble,UCPError,SMPPError,

mo1emg1,"=weeknum(d2,2)","=weekday(d2,2)",07Oct2007,iHUB-MO1EMG,"=time(hour(g2),0,0)",03:55,03:59,"=h2-g2",17286,12921,4365,0,0,0,12607,4363,0,0,124,4,0,0,0,0,

mo1emg1,"=weeknum(d3,2)","=weekday(d3,2)",07Oct2007,iHUB-MO1EMG,"=time(hour(g3),0,0)",04:00,04:04,"=h3-g3",16353,12217,4136,0,0,0,11839,4136,0,0,122,4,0,0,0,0,

mo1emg1,"=weeknum(d4,2)","=weekday(d4,2)",07Oct2007,iHUB-MO1EMG,"=time(hour(g4),0,0)",04:05,04:09,"=h4-g4",15762,11684,4078,0,0,0,11359,4077,0,0,123,8,0,0,0,0,

When i run my scripts under Cygwin, everything works fine and the output is as expected.

When i run this script under Solaris OS using /usr/bin/awk, the output is:

HOST,WEEK,WEEKDAY,day,node,HOUR,start,end,SAMPLING INTERVAL,TotalRequest,MSRequest,NWIRequest,SMSBrequest,SMSBack,SMSBNack,MSRequestSuccess,NWIRequestSuccess,SMSCCongestion,ApplicationCongestion,InvalidParameters,MSIllegalSubscriber,MWIllegalSubscriber,ApplicationUnavailble,UCPError,SMPPError,

,"=weeknum(d2,2)","=weekday(d2,2)",07Oct2007,iHUB-MO1EMG,"=time(hour(g2),0,0)",03:55,03:59,"=h2-g2",17286,12921,4365,0,0,0,12607,4363,0,0,124,4,0,0,0,0,

,"=weeknum(d3,2)","=weekday(d3,2)",07Oct2007,iHUB-MO1EMG,"=time(hour(g3),0,0)",04:00,04:04,"=h3-g3",16353,12217,4136,0,0,0,11839,4136,0,0,122,4,0,0,0,0,

,"=weeknum(d4,2)","=weekday(d4,2)",07Oct2007,iHUB-MO1EMG,"=time(hour(g4),0,0)",04:05,04:09,"=h4-g4",15762,11684,4078,0,0,0,11359,4077,0,0,123,8,0,0,0,0,

Here, looking at the 2,3,4th line, the first field (hostname) is not populated.

When i run the script using /usr/xpg4/bin/awk, i get the following error:

FIVEMIN_APPSMSHUB_OM200711071126.log
/usr/xpg4/bin/awk: file "/opt/redknee/traffic/scripts/OMParser.awk": line 2: /{/: syntax error Context is:
>>> =" "; printf("%s,", host)}
>>> $1 !~ /^#/ && $1 !~ /{/ <<<

Looking at the man pages for awk, i could make out that /usr/bin/awk does not support variable assignment (guessing). But when i tried to use /usr/xpg4/bin/awk, i get this error. I do not have an option to use gawk and have to use the awk that is available by default in SUN OS. Any idea what might be going wrong? I wish to have the value of a variable (hostname in my case) i use in my script to be populated within the AWK script. Is there any other way to do that?

Help is much appreciated.
-Deepak

Enclose the variables in single quotes.

Regards

On Solaris the default awk is /usr/bin/awk i.e the original (and old) awk, AKA oawk on some systems. The default awk does not support the -v assignment option.

To use a more modern version of awk on Solaris, you can use either /usr/bin/nawk (new awk) or /usr/xpg4/bin/awk.

The problem with you awk script is that you are not escaping the left brace in your ERE (extended regular expression). Braces have special meaning in EREs.

Change

  $1 !~ /^\#/ && $1 !~ /\{/ && $1 !~ /\}/ && $2 !~ /\{/ \{

to

 $1 !~ /^\#/ && $1 !~ /\\\{/ && $1 !~ /\}/ && $2 !~ /\\\{/ \{

In nawk you should take care that -v should be entered after '' and not before.

That was awesome and quick! All i had to do was fix the opening braces and everything is working fine with /usr/xpg4/bin/awk!

Really appreciate your help guys. Thanx to everyone.
-Deepak