Click to See Complete Forum and Search --> : Quick awk question.


Linux_cat
01-13-2005, 06:46 AM
quick awk question:

When using awk in a script how do you print a variable e.g

nawk '{$5 = $variable}'
I keep getting errors!!!.

the.spike
01-13-2005, 09:20 AM
What sort of errors are you getting?

Looking at that code you seem to be trying to set $5, which will be the fifth field of the input row to $variable.

Printing in nawk is normally done with print and printf.

spike..

Linux_cat
01-13-2005, 09:33 AM
Thanks for replying, the actual block of code goes like this

TY=$(echo $line_text|nawk -F'[|-]' '{print $5}') # Year for today - MUST be YYYY
TM=$(echo $line_text|nawk -F'[|-]' '{print $6}') # Month for today
TD=$(echo $line_text|nawk -F'[|-]' '{print $7}') # Day for today

JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD+7)) )
# parse yesterdays date string
set - $(echo $yesterdays_date_str)
new_date=$(printf "%d-" $3
printf "%02d-" $1
printf "%02d-00:00\n" $2)
echo $new_date
echo $line_text|nawk 'BEGIN{FS="|"; OFS="|"}{$5 =$new_date; print}'


its the last line that is causing me trouble, it keeps interpreting $ as a field and I keep getting errors, the date is formatted -2002-04-23-00:00 so need the right quotes as its not a straight integer...any ideas?

Linux_cat
01-13-2005, 09:59 AM
ive figured it!!!!!
"'$variable'"


DEEEEEEEPPPPPPP.

Thanks mate.

bradfordgd
01-13-2005, 12:56 PM
I think you could also change your last line to this;

echo $line_text|nawk -F| '{print $5}'

for the same results