variables with space causes errormessages?! 
Author Message
 variables with space causes errormessages?!

Hi *

I'm awking an performance report.
In the function below i use the shell variable pk, which is filled with
a
date time stamp, eg
# echo $pk
22 Sep 2000 12:00:23

Function looks like this:
function fill (databasefield,value) { print "update performance \nset
"databasefield"="value ; pk
="'$pk'" ; print "where pk=\""pk"\"\ngo" }

the nawk gives a errormessage:

nawk: syntax error at source line 2 in function fill
 context is
        function fill (databasefield,value) { print "update performance
\nset "databasefield"="value
; >>>  pk="22 <<<
nawk: illegal statement at source line 2 in function fill
        missing }

if pk is filled withhout spaces, there is no problem. But, allas, Tao
would have liked it: I need
those empty spaces :-( any help...

Tnx



Tue, 11 Mar 2003 03:00:00 GMT  
 variables with space causes errormessages?!

Quote:

> I'm awking an performance report.
> In the function below i use the shell variable pk, which is filled with
> a
> date time stamp, eg
> # echo $pk
> 22 Sep 2000 12:00:23

> Function looks like this:
> function fill (databasefield,value) { print "update performance \nset
> "databasefield"="value ; pk
> ="'$pk'" ; print "where pk=\""pk"\"\ngo" }

That's a partially quoted part of a shell script, I guess. Try:

  ="'"$pk"' where pk=\""'"$pk"'"\"\ngo" }

for that last line.

Quote:
> if pk is filled withhout spaces, there is no problem. But, allas, Tao
> would have liked it: I need
> those empty spaces :-( any help...

Normally you put your awk scripts in separate files, because then you
have much less trouble with shell quoting. "Optimizing for
maintainability"...

If that's an update statement for an SQL database:

 function fill (databasefield,value)
 {
   print "update performance "
   print "  set " databasefield "='" value "', pk='" pk "'"
   print "  where pk='" pk "'"
   print "go"
 }

Put that and the rest of your awk script in a file your_awk_script.awk
and run nawk so:

 nawk -v pk="$pk" -f your_awk_script.awk

Regards...
        Michael



Tue, 11 Mar 2003 03:00:00 GMT  
 variables with space causes errormessages?!

Quote:

> Hi *

> I'm awking an performance report.
> In the function below i use the shell variable pk, which is filled with
> a
> date time stamp, eg
> # echo $pk
> 22 Sep 2000 12:00:23

> Function looks like this:
> function fill (databasefield,value) { print "update performance \nset
> "databasefield"="value ; pk
> ="'$pk'" ; print "where pk=\""pk"\"\ngo" }

> the nawk gives a errormessage:

> nawk: syntax error at source line 2 in function fill
>  context is
>         function fill (databasefield,value) { print "update performance
> \nset "databasefield"="value
> ; >>>  pk="22 <<<
> nawk: illegal statement at source line 2 in function fill
>         missing }

> if pk is filled withhout spaces, there is no problem. But, allas, Tao
> would have liked it: I need

Hello,

the Shell form which you are calling your awk splits your awk-programlines
into more than one parameter
and therefore your code gets broken.
Assing the value of the shell-variables to awk-variables and arrage your
code to use these, like:

awk -v pk="$pk" -v var2="$..." 'AWK-commands'  files...

and change your code from
{...field="prefix'"$pk"'suffix"...}
to
{...field="prefix"pk"suffix"...}

This will work.

mfg nik

Quote:

> those empty spaces :-( any help...

> Tnx

--
Quote of the month:
        Perl can do everything Awk can do, but not as elegantly.


Tue, 11 Mar 2003 03:00:00 GMT  
 variables with space causes errormessages?!

 ...

Quote:
> That's a partially quoted part of a shell script, I guess. Try:
>   ="'"$pk"' where pk=\""'"$pk"'"\"\ngo" }

 ...

Quote:
> Normally you put your awk scripts in separate files, because then you
> have much less trouble with shell quoting. "Optimizing for
> maintainability"...

 Of course you can also use a "here" document which embeds your
 awk script into your shell script optionally eliminating the
 extra layer of quoting while keeping the single script in one
 place (so you can readily move and/or copy the script around without
 "packaging" it).

 Here's an example:

        awk -f - other args go here ... <<-'ENDOFSCRIPT'
                # you can put your awk script here
                # leading whitespace is ignored (due to the
                # - dash after the << operator
                # And all $ and backtick operators which
                # would normally be evaluated/interpolated
                # in double-quoted shell screens are ignored
                # because our "end of here doc" token was
                # quoted.
        ENDOFSCRIPT

        # rest of our shell script goes here.
        # notice that we MUST use -f - (the dash parameter/argument
        # is treated as a special filename signifying stdin).

 The treatment of -f - (the dash as a filename argument to
 the -f switch) might be dependent on your version of awk).

 Of course this won't work as is if you want to actually use
 the shell script as a filter and you want the awk script to
 read from the shell's stdin.  I think you'd have to be much more
 clever with your file desciptors in that case.  On some
 systems you could use something like

        awk -f /dev/fd/5 .... 5<<-'EOS'
                # awk script here ...
        EOS
        # rest of shell script here

 ... assuming that your version of UNIX offers a /dev/fd/
 feature (under Linux that's usually a symlink from
 /dev/fd --> /proc/self/fd) and that your shell allows you
 to specify a file descriptor as part of your here doc
 (works under bash 2.04 under Linux; since I just tested it).
 This /dev/fd/* and 5<<-* stuff should be completely independent
 of your version of awk.



Tue, 18 Mar 2003 11:50:09 GMT  
 variables with space causes errormessages?!

Quote:
>  Of course this won't work as is if you want to actually use
>  the shell script as a filter and you want the awk script to
>  read from the shell's stdin.  I think you'd have to be much more
>  clever with your file desciptors in that case.  On some
>  systems you could use something like

>    awk -f /dev/fd/5 .... 5<<-'EOS'
>            # awk script here ...
>    EOS
>    # rest of shell script here

Wow, that's cute! I never figured out how to do that. And all my shells
(bash, ash, pdksh, zsh) support that syntax.

Are we guaranteed that we do have /dev/fd/5 available in our shell
script? What if we get called from another script that already uses that
particular file descriptor? Do we need to find an unused /dev/fd/* and
if so, how can we do that?

Quote:
>  ... assuming that your version of UNIX offers a /dev/fd/
>  feature (under Linux that's usually a symlink from
>  /dev/fd --> /proc/self/fd) and that your shell allows you
>  to specify a file descriptor as part of your here doc
>  (works under bash 2.04 under Linux; since I just tested it).
>  This /dev/fd/* and 5<<-* stuff should be completely independent
>  of your version of awk.

IIRC gawk emulates the /dev/fd/* if it isn't "physically" there, so
probably we only need a shell that understands the "5<<" syntax (and
gawk of course). I'll try it on Monday with Solaris and Compaq's Tru64.

Regards...
                Michael



Tue, 18 Mar 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Fortran program causing a Windows thread space error R6016

2. local variables don't cause side effects

3. DVF 6.0 Optimization causes debugger to ignore variables

4. GNAT errormessage question

5. Cookie gets me an errormessage I don't understand

6. Errormessages

7. complaint about tcl errormessages

8. Global variables at the Address Space level

9. variable with space

10. variable with many space

11. Variables with *WHITE SPACE*..

12. removing trailing spaces from string variables

 

 
Powered by phpBB® Forum Software