Affected shell variable in awk
Hi
I try to assign a variable in awk previously defined in my script, but I can not find the correct syntax:
Code:
/ bin / bash
var = 2
awk-F '' '
BEGIN (
tata = $ 1
$ var = tata
)
END '
# # Here, the script continues, and var is inside tata.
Could you help me with line number 7?
Re: Affected shell variable in awk
Hi
I think your calls are not clear:
Code:
varBash = 2
awk-F ''-v varAwk = $ varBash '(uneVar = $ 1; varBash = uneVar; print uneVar, varBash, varAwk)' <<< "this is an example"
Think you should correct this and I suggest you to keep your code clean so that it can to readable to you in future.
Re: Affected shell variable in awk
Hi
Note that the $ are used for variables "columns" ($ 0 is the complete line, $ 1, $ 2 ... being the columns) so when you do $ var = tata, it's like if you replaced it by var = $ 0.
You have to do is simply
Code:
var = tata to affect the contents of the variable in the variable tata
and do var = "tata" to assign the string variable in tata var
Hope this helps you. If nay more queries then do post back.
Re: Affected shell variable in awk
Hello,
I said my application in simplifying the example:
Code:
/ bin / bash
var = 2
awk-F '' '
BEGIN (
var = 3
)
END '
# # Here, the script continues, and var is 3.
Is this clearer now? Any advice now?
Re: Affected shell variable in awk
Hello
You can also do it like say, block the last statement END of your procedure AWK is add the following line:
Code:
END (
....
....
....
printf ( "% s", var);
)
And in your script you call your awk
Code:
VAR = `awk-f script.awk ....`
echo "VAR = $ VAR"
This will help for sure!
Re: Affected shell variable in awk
Hi
Thank you for your solution. I confess that I use the same exit (which limits the return has a numeric value less than 256), but I was looking if there was another way of doing things. I do not think this is possible, by the fact that the awk process can access the memory space of the script. Thanks again.