Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command.

I tried three different nawk command as below but none of them substitute the value of entry variable.

 
 ls *.txt  | while IFS='' read -r entry; do
#nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)} /<\/name>/{A=0} ' $entry
#nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"'$entry')} /<\/name>/{A=0} ' $entry
#nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"'"$entry"')} /<\/name>/{A=0} ' $entry
done
 

Please suggest.

You would need to use double-quotes rather than single quotes so that the shell gets in and expands them for you. This may, however, cause issues with the existing double-quotes.

You can try changing them to single quotes or escaping them.

Does that help?

Robin

I am not sure how can I use double quotes instead of single.

But this is what I tried and it still fails.

nawk "/<name>/{A=1;++i} A{print >> ("cmd"i"$entry")} /<\/name>/{A=0} " $entry

It would help to show the error returned, however what you are doing with the command like this is to call nawk with confused arguments because you have double quotes throughout.

Does this perform any better?:-

nawk "/<name>/{A=1;++i} A{print >> ('cmd'i'$entry')} /<\/name>/{A=0} " $entry

It might complain still, but please show us the output, else we're just guessing blind.

Kind regards,
Robin

I tried your last suggestion but it fails with the below error.

bash: syntax error near unexpected token `('
bash-3.2$ uname -a
SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v

Don't mess around with single/double quotes and shell variable expansion; use the designated method:

ls *.txt  | while IFS='' read -r entry; do
awk -vENTRY="$entry" '/<name>/ {A=1; ++i} A {print >> ("cmd" i "_" ENTRY)} /<\/name>/ {A=0} ' $entry
done
2 Likes

with this correction in RudiC's suggestion ...it works !!

awk -v ENTRY="$entry" '/<name>/ {A=1; ++i} A {print >> ("cmd" i "_" ENTRY)} /<\/name>/ {A=0} ' $entry

Still i would like to try rbattle1's suggestions if that gets fixed.

In addition to what RudiC has already suggested, since the variable you want to use is the name of the file being processed by your awk script, you could more simply just use:

nawk '/<name>/{A=1;++i} A{print >> ("cmd" i "_" FILENAME)} /<\/name>/{A=0}' "$entry"

but you may run out of file descriptors (especially with nawk ) depending on how many output files you are creating. You could avoid that problem using:

nawk '/<name>/{A=1;++i} A{print >> (ofn = "cmd" i "_" FILENAME)} /<\/name>/{A=0;close(ofn)} ' "$entry"
1 Like

You could do it the way you were trying to do it with:

nawk '/<name>/{A=1;++i} A{print >> (ofn = "cmd"i"_'"$entry"'")} /<\/name>/{A=0;close(ofn)}' "$entry"

but, due to the complicated nesting of quotes, it is harder to read, easier to make mistakes, and harder to debug when it doesn't work.
Or, the way rbatte1 was trying to do it with:

nawk "/<name>/{A=1;++i} A{print >> (ofn = \"cmd\"i\"_$entry\")} /<\/name>/{A=0;close(ofn)}" "$entry"

but this form gets even harder to read if you have an awk script that uses both shell variables and the contents of awk fields.

Why, then, don't you answer his request?

1 Like