Hi All, I am using command awk ‘{print ""put $LOC “” $0}’, when I am executing the script $LOC is not resolving.Please suggest. Thanks

Both suggestions are useful ( define awk-variable with -v option; let shell to resolve the variable in double-quoted string)
One more approach: switch between single and double quotes:

awk ‘{print "put "’$LOG’" " $0}’ inputfile
Alright, here is no needs to use both (single - enough), but it is possible. I hope it is clear: the single quot string is completed just before the variable and right after it is presented (the variable for the shell), the single quotes are re-started.

Jalal,

Not clear which post you are suggesting needs modified.

If the original, can you explain how:

awk ‘{print "put $LOC " $0}’

is going to work, being as the $ is already inside single quotes anyway?

[ENDS]

Hi,

Shell wants to resolve $VARIABLE…
use “” before $ sign…

Missed that one, sorry.
Antonio Vasconcelos
CGD/SSI/ASD/SO - Sistemas UNIX - JXXI piso 7 - ext: 55 67 81

Antonio,

Slight omission in your suggestion.

The $0 also needs to be escaped as $0. If not, the shell substitutes the process name of the outer shell (either the shell script name, or literally “ksh”, depending on which shell and system). The dollar never makes it into awk to be interpreted as the current line.

It is so hard to get internal quoting right if you double-quote the whole awk program, and so hard to debug and maintain the resultant hieroglyphics, that I gave up ever trying to do it. I always single-quote the awk program.

My post 13 days ago above gives 4 safe ways to inject shell variables into awk programs.

Pravin, good shot but liable to fail. You have to quote arguments introduced with -v, as in -v “var1=$LOC”. The issue comes where LOC has multiple words, like “One Two”. Without the quotes, the shell sets var1 to One, uses Two as your awk program (which produces no output because ‘Two’ is an empty string in the awk), and fails trying to treat your actual awk script as the first data filename.

[ENDS]

The problem are the ’ (single quotes) around the awk command.
Change that to " (double quotes) and add an (backslash, escape char) just before each internal ".
So, this should work

awk "{print “put $LOC " $0}”

Ant?nio Vasconcelos
CGD/SSI/ASD/SO - Sistemas UNIX - JXXI piso 7 - ext: 55 67 81

You can use -v option with awk.
awk -v var1=$LOC ‘{print "put “var1” " $0}’ inputfile

$LOC does not mean anything inside awk. $ is syntactically followed by a number that selects a field on the current input line. $0 selects the whole line.

If LOC is intended to be a shell variable, you need to get it inside awk somehow. Typically, use one of these four methods (there are others):

(a) If it is exported in shell, refer to it as ENVIRON[“LOC”] in awk.

(b) Initialize a var called LOC in the command line with awk -v LOC=“${LOC}” …

(c) Break back out into shell quotes with:

awk ‘{print "put "’"$LOC “'” $0}’

Those repeated quotes are each double single double. The outer doubles surround an awk string. The next inner singles match those around the whole awk program, so between them you are in shell land. The inner “$LOC” is a normal shell substitution.

(d) You could put LOC on the front of your input, as with:

{ echo “${LOC}”; cat whatever; } |
awk ‘NR == 1 { LOC = $0; next; }
{ print LOC, $0; }’

[ENDS]