General
cancel
Showing results for 
Search instead for 
Did you mean: 

SCRIPT variable in awk' find pattern

SOLVED
Go to solution
Gunwant
Occasional Advisor

SCRIPT variable in awk' find pattern

Hi

I want to use script variable in awk' find pattern.
///Code

WHENEVER="rstatd"
awk -v VAR=$WHENEVER 'BEGIN { FS="#"} /.*'VAR'.*/ { $0=$2 }; { print }'

I want to use variable VAR in "/.*'VAR'.*/"
so that i can make this script parametrize
and use repeatedly.



but it cannot replace the VAR variable value in the regular expression.



I tried all possible ways, for example
WHENEVER="rstatd"
cat /etc/inetd.conf|awk -v VAR=$WHENEVER 'BEGIN { FS="#"} /.*'$VAR'.*/ { $0=++a ":"$2 ;print VAR}; { print }' >/etc/inetd.conf.bl
##

WHENEVER="rstatd"
cat /etc/inetd.conf|awk -v VAR=$WHENEVER 'BEGIN { FS="#"} /.*'"$VAR"'.*/ { $0=++a ":"$2 ;print VAR}; { print }' >/etc/inetd.conf.bl
##
WHENEVER="rstatd"
cat /etc/inetd.conf|awk -v VAR=$WHENEVER 'BEGIN { FS="#"} /.*'$VAR'.*/ { $0=++a ":"$2 ;print VAR}; { print }' >/etc/inetd.conf.bl

it print the output without replacing the matching line.

If somebody can put a light on it.

Thanks


Don't wait be create
2 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: SCRIPT variable in awk' find pattern

Hi:

Try this:

# WHENEVER=rstatd;
# awk -v VAR=$WHENEVER 'BEGIN{FS="#"} $0~VAR {$0=$2};{print}' /etc/inetd.conf

...Notice too, that 'awk' will read the file without the need to 'cat' and pipe it. This eliminates an extra process!

Regards!

...JRF...
Gunwant
Occasional Advisor

Re: SCRIPT variable in awk' find pattern

Thanks

It works

final awk command is


PARAM="rstatd"
awk -v VAR="^#.*$PARAM.*" 'BEGIN{FS="#"} $0~VAR {$0=$2};{print}' /etc/inetd.conf>/etc/inetd.conf.bl
Don't wait be create