Well, awk doesn't seem to be any smarter, to be honest. Or am I mistaken?
Code:
root@kube:~# cat file.txt Of course, a normal string substitution works fine: Code:
root@kube:~# cat file.txt |
Quote:
|
you may need to export variables, otherwise (as in your example) that will be again evaluated by the shell.
sub (in awk) works on regexp, that's why it replaced the end of the line (when var=$) If you wish to use simple substitution/replacement you need to find another solution. Code:
awk -v a="${var}" -v b="${var2}" { |
Right, that's correct, this is unrelated to any interpretation of the bash variables.
What about this? Now I'm curious, while we're at it: Code:
root@kube:~# echo $var EDIT: I've posted it without seeing pan64's latest reply. |
which awk is it exactly? what is in file.txt?
|
It's gawk (comes with most Linux distros - here running under Ubuntu 18.04)
In file.txt there's only "crap2". I couldn't get your command to work, I'm not sure why. I get this: Code:
awk: cmd. line:1: { |
Quote:
|
Quote:
Code:
awk -v a="${var}" -v b="${var2}" '{ |
It doesn't work either way :) Adding double quotes shouldn't work because they are already being used, and it might screw up the interpretation. Single quotes don't work anyway. I placed the whole command in a file, and it's not working.
|
Quote:
|
I've just tried it out, it works:
Code:
$ cat file.txt |
Yes, I missed that. It does work as expected, indeed!
Code:
root@kube:~# cat file.txt But if I wanted to use regex for matching the pattern and then substitute it with a literal string, like the sub command does, then all of a sudden I have a problem, in that I yet again have to be careful about what the variable contains, right? |
Yes, and you should be aware of how regex in a string differs from a regex literal between slashes.
|
and additionally you may also kill the awk process (with an invalid regexp).
|
Right, but then I return to the initial problem related to sed and the interpretation of bash variables and I see that awk is susceptible to very similar issues, regardless of when or by what the variable is converted into its value.
Maybe perl would do the job, but I don't see lots of people writing perl substitutions in their docker entrypoints, to be honest. Installing python to create entrypoint containers could be ok, depending on the situation, but normally you don't want your image to grow by hundreds of mb just for a substitution. |
All times are GMT -5. The time now is 04:59 PM. |