awk Print get error

Hi, Gurus,
I have script as following:

#!/usr/bin/khs
lastdate=`cat abc`
echo $lastdate
awk '{ if ($0~/^\$\$lastupdatedate *=/) {print '\$\$lastupdatedate=$lastdate'} else {print $0}}' file1 > tmp

error message

awk: syntax error near line 1
awk: illegal statement near line 1

when I change $lastdate to '2001-01-01 00:00:00', code as following:

#!/usr/bin/khs
lastdate=`cat abc`
echo $lastdate
awk '{ if ($0~/^\$\$lastupdatedate *=/) {print "$$lastupdatedate=2001-01-01 00:00:00"} else {print $0}}' file1 > tmp

:wall:

Any body can help me out

Thanks in advance

That's a useless use of backticks. Why do "var=`cat stuff` ; echo $var" when just "cat stuff" achieves the same thing?

It's valid in my awk. Depending on your system, 'awk' may actually be an ancient feature-deprived version, try nawk.

I'm not sure it's actually doing what you want either, though. That $$ may be turning into a number, the shell's PID.

There's an easier and safer way to get variables into awk/nawk than fooling with single/double quotes:

echo asdf | awk -v var="qwerty" '{ print $1, var }'

---------- Post updated at 01:23 PM ---------- Previous update was at 01:15 PM ----------

Or even get the var in awk itself:

awk 'BEGIN { getline var <"abc" } { print $0, var }'

Also, you probably should fix your shebang line:

#!/usr/bin/ksh

[/COLOR]
[/COLOR]

Thanks both of you.
My situation is:
In my first file abc contains one string '2011-01-01' (only on line), in my second file contains multiple lines, one of them is $$lastupdatedate=xxxx-xx-xx, I want replace xxxx-xx-xx with 2011-01-01 in second file and get $$lastupdatedate=2011-01-01.

Any body can help me out.

Thanks in advance

#!/bin/ksh

dateFile=~/test/date.txt
fileToReplace=~/test/input.txt

awk -F= '/^\$\$lastupdatedate/ {$2="'`cat $dateFile`'"}1' OFS="=" $fileToReplace

the regex will match a line starting with '$$lastupdatedate', and replace the second field (stuff after equal sign) with the contents of $dateFile. '1' at the end is just a shortcut to print everything out.

Thanks Mirni for your detail explanation.

It works perfectly.

---------- Post updated 06-23-11 at 10:51 AM ---------- Previous update was 06-22-11 at 09:10 PM ----------

Hi, Mirni,
Above scripts works fine when I tried on my laptop. But it give me error when I tested on my workstation.
awk: syntax error near line 1
awk: illegal statement near line 1
the system as following
$ uname -a
SunOS abc 5.10 Generic_144488-11 sun4v sparc SUNW,Sun-Fire-T200

any idea about this?

Thanks in advance

Use nawk on Solaris.

Hi, Shamrock,
after changing to nawk, I got following errors.
nawk: can't open file OFS==inputfile
I removed OFS="=", but I got another error as following

:nawk: syntax error at source line 1
 context is
        /^\$\$lastupdatedate/ >>>  {$2="2010-01-01 <<<
nawk: illegal statement at source line 1

any idea about this?

Thanks in advance.

another thing, is there any other way to achieve this purpose?