This is not really an Awk question as Awk can do nothing to access shell variables.
The usual solution is to have the shell expand that variable before the code is passed to Awk. But parameter expansion is not performed on single quoted ( ' ) strings :
So to make quoting work as you wish, you have to either :
Code:
[gray]# interrupt the single quotes around the shell variable and quote it with double quotes[/gray]
awk '$1 ~ /[highlight]'[red]"[/red][/highlight]$I[highlight][red]"[/red]'[/highlight]/ {print $1, $2}'
[gray]# escape everything you not want the shell to expand[/gray]
awk "[highlight]\[/highlight]$1 ~ /$I/ {print [highlight]\[/highlight]$1, [highlight]\[/highlight]$2}"
Beside those the are 2 more ways the shell can make a value accessible to Awk :
Code:
[gray]# define an Awk variable from outside, using the [b]-v[/b] ( or [b]--assign[/b] ) option[/gray]
awk [highlight]-vJ="$I"[/highlight] '$1 ~ [highlight]J[/highlight] {print $1, $2}'
[gray]# export the variable ( using the [b]export[/b] shell builtin ) so Awk can access it as environment variable[/gray]
awk '$1 ~ [highlight]ENVIRON["I"][/highlight] {print $1, $2}'
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.