calling external values inside awk command

I have a code as follows

awk ' BEGIN{FS=OFS=","}
{
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1[i],a2[i],a3[i],date
}
}' file

In the above code I need to add current date(date). How is this possible to call an date or a exported value inside awk.

awk -v date=$(date +%Y%m%d) 'BEGIN ...

Adjust the command to print the date in the format you want it. +%Y%m%d looks like 20080416 -- see the date manual page for the available options.

awk -v RD=$(date +%Y%m%d) 'BEGIN{FS=OFS="|"}
{
n=split($7,B_ID,"~") split($8,B_VAL,"~") split($9,B_COS,"~")
for(i=1;i<=n;i++)
{
print $1,$2,$3,$4,$5,$6,B_ID[i],B_VAL[i],B_COS[i],$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$
28,$29,$30,$31,$32,$35,$36,$37,$38,$RD
}
}' $Source_Path/EServe_0CCS_IN_${FD_WH}$HR1$HR2.TXT > $Target_Path/R_GPC_$3_${HR1}${HR2}.dat

It gives me an error

syntax error at line 9: `(' unexpected

The syntax error appears to be unrelated. Please put that inside

 tags so it's easier to see what's where.  Looks to me like you have only 8 lines; did you cut out other stuff and if so which line is line 9?

If your shell doesn't understand $(...) try with backticks instead;

RD=`date +%Y%m%d`

By the way, in the awk script, you want RD, not $RD

You can also use getline to retrieve the current date as in the following example

BEGIN {
    "date" | getline cur_date
    close("date")
}
{
     print cur_date
}

Dear era

     Here is the code 
awk -v rd=`date` ' BEGIN{FS=OFS=","}
{
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1,a2,a3
}
}' Myin.txt

This give me following error

awk: syntax error near line 1
awk: bailing out near line 1

If I remove the bolded code,its working properly?

if on Soalris, use either /usr/bin/nawk or /usr/xpg4/bin/awk

You need to put double quotes around the value if it contains spaces (which just plain date does; that's why I suggested using the +%Y%m%d format).

awk -v rd="`date`" '...'

Hai

I got the solution. But there is small problem

nawk -v rd=`date +%Y"-"%m"-"%d" "%T` ' BEGIN{FS=OFS=","}
{
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1,a2,a3,rd
}
}' Myin.txt

When I try to seperate date and Time stamp using Quotes, its giving an error
nawk: syntax error at source line 1
context is
>>> 19: <<<
nawk: bailing out at source line 1

My final out put of rd is YYYY-MM-DD HH:MI:SS

Use a unique delimiter to separate the date from the time and then remove that delimiter with a space inside the action part of the nawk script.

nawk -v rd=`date +%Y-%m-%d::%T` 'BEGIN{FS=OFS=","}
{
sub("::"," ",rd)
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1,a2,a3,rd
}
}' Myin.txt

Repeat: if the value contains spaces, you need to double-quote the whole of it.

nawk -v rd="`date +%Y"-"%m"-"%d" "%T`" 'BEGIN ...'

you don't need to go through this pain.....
as era's noted previously....

nawk -v rd="`date '+%Y-%m-%d %T'`" 'BEGIN...' 

Unfortunately era's code has awk on my machine still complaining about the space that's why I came up with that alternative :cool:

rd = `date +%Y-%m-%d" "%H:%M:%S`
print $rd