Operating System - HP-UX
1748129 Members
3457 Online
108758 Solutions
New Discussion юеВ

Re: passing script variable in awk .

 
Gunwant
Occasional Advisor

passing script variable in awk .

Hi,

I want to pass a script variable in to the awk command. The script variable is a message string having blank spaces.
$BANNER="Authorized users only. All activity may be monitored and reported."

The awk command is

ND_BANNER=`echo $BANNER |sed 's/\./\+/g'`
NB_BANNER=`echo $ND_BANNER |sed 's| |_|g'`

cat $FILE | awk VAR="${NB_BANNER}" 'BEGIN{ FS=":" ;OFS=":" };{ if($1~"^[ ]*Dtlogin.*greeting\.persLabelString"){ sub("+"," ",VAR); $2=" "VAR;}{print;}}'

I am replacing blank space with "_" sign and "." with "+".

I am getting following error.

syntax error The source line is 1.
The error context is
>>> VAR=Authorized_users_only._All_activity_may_be_monitored_and_reported+ <<<
awk: Quitting
The source line is 1.

I cannot find out why this is giving error.

Thanks.
Don't wait be create
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: passing script variable in awk .

I should have looked closer. Your syntax is wrong, you need -v and quote the "+":

NB_BANNER=$(echo $BANNER | sed -e 's/\./\+/g' -e 's| |_|g')

awk -v VAR="${NB_BANNER}" '
BEGIN{ FS=":" ; OFS=":" }
{ if($1~"^[ ]*Dtlogin.*greeting\.persLabelString"){
sub("\+", " ", VAR)
$2=" " VAR
}
{print}
}' $FILE
Gunwant
Occasional Advisor

Re: passing script variable in awk .

Hi Dennis,
Thanks for your reply.
Sorry , my mistake .

I did changes in the above command

NB_BANNER=$(echo $BANNER | sed -e s/\./11/g' -e 's| |00|g')

cat "/etc/dt/config/C/Xresources" | awk -v VAR="${NB_BANNER}" 'BEGIN{ FS=":" ;OFS=":" };
{
if($1~"^[]*Dtlogin.*greeting\.persLabelString")
{ sub("00"," ",VAR);
sub("11"," ",VAR);
$2=" "VAR;
}
{print;}
}'

I just used 00 for space and 11 for ".".
It gives following error message


awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The source line number is 1.
The error context is
BEGIN{ FS=":" ;OFS=":" };{ if($1~"^[ ]*Dtlogin.*greeting\.persLabelString"){ print VAR ;sub("+"," >>> ",VAR) <<<

Don't wait be create
James R. Ferguson
Acclaimed Contributor

Re: passing script variable in awk .

Hi Gunwant:

You collapsed the space within '[ ]'. Instead of:

if($1~"^[]*Dtlogin.*greeting\.persLabelString")

...you should have:

if($1~"^[ ]*Dtlogin.*greeting\.persLabelString")

The use of '[ ]' makes a space character more clearly distinguishable in 'awk'.

Regards!

...JRF...

Gunwant
Occasional Advisor

Re: passing script variable in awk .

Hi James,
Thanks for reply.

I inserted a space in those brackets .

NB_BANNER=$(echo $BANNER | sed -e s/\./11/g' -e 's| |00|g')

cat "/etc/dt/config/C/Xresources" | awk -v VAR="${NB_BANNER}" 'BEGIN{ FS=":" ;OFS=":" };
{
if($1~"^[ ]*Dtlogin.*greeting\.persLabelString")
{ sub("00"," ",VAR);
sub("11"," ",VAR);
$2=" "VAR;
}
{print;}
}'

but it is giving same result , same error message

after replacing correctors , the banner string looks like this


Authorized00users00only1100All00activity00may00be00monitored00and00reported11

Thanks
Don't wait be create
Dennis Handly
Acclaimed Contributor

Re: passing script variable in awk .

>It gives following error message:
awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression

If you look at my changes and my reply you'll see I already told you I fixed it.
awk takes ERE and you need to quote the naked "+".
If you format ALL of your awk programs with separate lines like I do, it points right at it.

Gunwant
Occasional Advisor

Re: passing script variable in awk .

Thanks , i got it
Don't wait be create