How to solve awk: line 1: runaway string constant error?

Hi All !

I am just trying to print bash variable in awk statement as string
here is my script

n=1
for file in `ls *.tk |sort -t"-" -k2n,2`; do
ak=`(awk 'FNR=='$n'{print $0}' res.dat)`

awk '{print "'$ak'",$0}' OFS="\t" $file
n=$((n+1))
unset ak
done

I am getting following error

awk: line 1: runaway string constant

is there any solution to read reference file first line print with 1st file(in loop) $0, then read 2nd line from reference file print with 2nd file in loop

It's not a good practice to use shell variables directly in awk. Pass the shell variables to awk using -v.

Check this example here(run away string problem)..

Guru.

Why is it normally not a good idea to use shell vars in awk? Is it because of the single quotes hiding the variable? Or some other reason?

Its mainly because of the quoting issues present in a shell variable. Refer this link for more: Using Shell Variables - The GNU Awk User's Guide

Guru.

Thanks.

awk 'NR=='"$n"' { print  }' filename

Hi,

Can you please help me to find/display out last Friday's date of the month using awk command in Unix/Linux.

i have found this command and its work

cal 03 2013 | awk 'NF>5{last = $6} END{print last}'

But it show the output in single digit like :29

But i need the output will shown in this format (Fri Mar 29 2013)

Please help.

avig,

Start your own thread for your help requests. Do not hijack another's thread. It is rude, inconsiderate, and creates confusion in the conversation.

I urge members to not reward this or other thread hijacks with helpful responses.

Regards,
Alister