Variable substitution in awk

Hi,
I have a variable to be substituted in awk. I am using AIX 5.3. Here is my piece of code:

REPL_DT=`date +'%Y\\\\\\\\\/%m\\\\\\\\\/%d'`
NEW_LINE=$(echo $Line | awk '{sub ($4, '$REPL_DT'); printf "# %-7s %9s %18s\n", $2,$3,$4}')
sed $n" s/.*/$NEW_LINE/" kfile > tmp
mv tmp kfile

Here, the value of the variable REPL_DT is,

echo $REPL_DT
2010\\\/04\\\/12

The error I get in awk statement is as follows:

 Syntax Error The source line is 1.
 The error context is
                {sub ($4, >>>  2010\ <<< \\\\); printf "# %-7s %9s %18s\n", $2,$3,$4}
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

When REPL_DT is only 2010 without any '\\\' it gets substituted correctly. I think the slashes after 2010 is causing problem. Any idea how to deal with this.

Thanks in advance,
Sugan.

---------- Post updated at 11:03 PM ---------- Previous update was at 09:41 PM ----------

I replaced the terminator in sed from '\' to '|' thinking it might help. Here is my piece of code:

REPL_DT=`date +'%Y/%m/%d'`
NEW_LINE=$(echo $Line | awk '{sub ($4, '$REPL_DT'); printf "# %-7s %9s %18s\n", $2,$3,$4}')
sed $n" s|.*|$NEW_LINE|" kfile > tmp
mv tmp kfile

It works but the problem is, instead of '2010/04/12', awk substitutes the value after dividing 2010/04 and then dividing the result by 12. It substitutes 41.83!!!

I tried substituting something like '2010\/04\/12' but it does not work in awk. Please help.

---------- Post updated 04-13-10 at 01:46 AM ---------- Previous update was 04-12-10 at 11:03 PM ----------

Hi,
I replaced the code a little to circumvent this problem. I used the following code and the problem is fixed...:slight_smile:

      NEW_LINE=$(echo $Line | awk '{sub ($4, '$REPL_DT'); print}')
      r_yy=`echo $NEW_LINE | cut -d " " -f4 | cut -c1-4`/
      r_mm=`echo $NEW_LINE | cut -d " " -f4 | cut -c5-6`/
      r_dd=`echo $NEW_LINE | cut -d " " -f4 | cut -c7-8`
      r_line=`echo $NEW_LINE | cut -d " " -f1-3`
      REP_LINE=`echo "$r_line" "$r_yy""$r_mm""$r_dd"`
      NEW_LINE=`echo $REP_LINE | awk -F " " '{printf "# %-7s %9s %18s\n", $2,$3,$4}'`
      sed $n" s|.*|$NEW_LINE|" kfile > tmp
      mv tmp kfile

Thank you very much.

Try something like this:

REPL_DT=`date +'%Y\/%m\/%d'`
NEW_LINE=$(echo $Line | awk -v var=$REPL_DT '{$4=var; printf "# %-7s %9s %18s\n", $2,$3,$4}')

Hi,
Your code is working fine. Thanks so much.