1825780 Members
2492 Online
109687 Solutions
New Discussion

awk print variable

 
Patrick Stevens
Occasional Contributor

awk print variable

Hello,

I need to something like the following:

2 variables eg

sedate=20040302
setime=130000

echo a string to create a new one:

echo "29Feb 14:21:15, ,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk -F, '{print $1","$sedate $setime","$3","$4","$5","$6","$7}'


However I have not got the handling of the variables right, as I get :


awk: Field $() is not correct.
The input line number is 1.
The source line number is 1.

Any help apreciated!

As I lay on my bed looking at the stars, I wandered where the hell is the ceiling?
7 REPLIES 7
Mark Grant
Honored Contributor

Re: awk print variable

try

printf "%s,%s %s,%s,%s,%s,%s,%s",$1,$sedate,$setime,$3,$4,$5,$6,$7

INstead of your "print" statement
Never preceed any demonstration with anything more predictive than "watch this"
Jean-Luc Oudart
Honored Contributor

Re: awk print variable

You must these variables to awk

awk -v var1=$setdate -v var2=$setime '{
print $1","var1","var2 ...

Regards,
Jean-Luc
fiat lux
Hein van den Heuvel
Honored Contributor

Re: awk print variable


You need to get those variables into awk. Jean-Luc shows a good solution to that using command line arguments.
An alternative is to use the ENVIRON array that awk provides. Something like (untested):
awk -F, 'print $1,ENVIRON["sedate"] ENVIRON["setime"],$3 ....

hth,
Hein.
curt larson_1
Honored Contributor

Re: awk print variable

the above poster have shown you how to get the variables into awk.

here are a couple of ways without using awk
using sed. be sure to use double quotes

print "29Feb 14:21:15, ,XXXXXXXX,FOS00001980" | sed "s/\(.*\),,/\1,$sedate $setime,/"

and just using the shell

string="29Feb 14:21:15, ,XXXXXXXX,FOS00001980"
last=${string#*,}
first=${string%%,*}
printf "%s,%s %s%s\n" $first $sedate setime $last
Rory R Hammond
Trusted Contributor

Re: awk print variable



SETDATE=20040302
SETTIME=140000
echo "29Feb 14:21:15,,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk '{print $1","SEDATE" "SETIME" ,"$3","$4","$5","$6","$7}' SEDATE=${SETDATE} SETTIME=${SETTIME}

or

echo "29Feb 14:21:15,,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk '{print $1","SEDATE" "SETIME" ,"$3","$4","$5","$6","$7}' SEDATE=20040302 SETTIME=140000


Rory
(I tested #2 not #1 but both should work)
There are a 100 ways to do things and 97 of them are right
Leif Halvarsson_2
Honored Contributor

Re: awk print variable

Hi,

Why don't you just echo also the variables into the pipeline ? Something like:

sedate=20040302
setime=130000

echo a string to create a new one:

echo "29Feb 14:21:15,$setdate,$setime,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk -F, '{print $1","$2","$3","$4","$5","$6","$7","$8}'

Kent Ostby
Honored Contributor

Re: awk print variable

Another way to do this would be:

echo "SEDATE",$sedate > myfile
echo "SETIME",$setime >> myfile
echo "29Feb 14:21:15, ,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" >> myfile

NOTE you could actually echo multiple of these last lines and have things work if you had a constant SEDATE and SETIME that you wanted to use and multiple "data" lines.

Then create an awk file called se.awk
/^SEDATE/ {sedate=$2;next;}
/^SETIME/ {setime=$2;next;}
{print $1","sedate setime","$3","$4","$5","$6","$7}

Then run:
awk -f se.awk myfile > myoutputfile

Best regards,

Kent M. Ostby

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"