Re: question on awk and sed

 
SOLVED
Go to solution
Charles Li_2
Occasional Advisor

question on awk and sed

is it possible to use $variable inside sed and awk:
svar="xyz"
sed -e 's/$tvar/x/'

avar=5
awk '{ print $'$avar' }'

Thanks,Charles
10 REPLIES 10
Steven E. Protter
Exalted Contributor
Solution

Re: question on awk and sed

For sure yes with sed.

You may need to use the structure ${tvar} first.

Why don't you just try it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Tom Danzig
Honored Contributor

Re: question on awk and sed

In awk the answer is yes.

For example:
BEGIN{
X=50
}
{
printf("%d\n",X)
}

Will print 50.

To pass variables from the shell into awk, use:

awk -v var=value

Charles Li_2
Occasional Advisor

Re: question on awk and sed

I would have, but I did not know the syntax. Should it be {},[],(),"",'' or ``.
Tom Danzig
Honored Contributor

Re: question on awk and sed

As Steven says, use curly braces arount the variable name to isolate it. i.e ${varname}
Ramkumar Devanathan
Honored Contributor

Re: question on awk and sed

Charles,

1. Enclose the regex within double quotes, and not single quotes.
sed -e "s/$tvar/x/" ...

2. Works fine for me.
awk '{ print $'$avar' }'
this works too -
awk "{ print \$$avar ;}" ...

- ramd.
HPE Software Rocks!
Charles Li_2
Occasional Advisor

Re: question on awk and sed

Tom,
I want to do the following with awk:

for loop
K=awk '{ print $$loop }'
## giving K a different value each time the loop is executed.
endloop

is this possible?
Charles Li_2
Occasional Advisor

Re: question on awk and sed

Works great. Thanks to all.
Tom Danzig
Honored Contributor

Re: question on awk and sed

Charls,

It should be. Remember not to preceed variables in the awk portion with a $. The shell uses the $ but not awk.
curt larson_1
Honored Contributor

Re: question on awk and sed

well I guess that depends on what you mean by "inside sed and awk". sed doesn't have support variables. But, it seems what your trying to do is shell expansion of a variable before it is sent to sed.

abc="abc";sed -e 's/$abc/def/'

the shell doesn't do variable substitution with single quotes. So, the above wouldn't do what your wanting. double quotes do, so either of these will do what your wanting to do.

abc="abc";sed -e "s/$abc/def/"
abc="abc";sed -e 's/'$abc'/def/'
once again sed isn't using the variable, the shell is expanding the variable before sed sees it.
abc="abc";sed -e "s/$abc/def/" gets expanded by the shell to
sed -e "s/abc/def/" before the sed command is ever called. So, your making use of a variable in the shell but sed never knows about it.

the same can be done with awk. But awk has methods to used variables and can be passed variables. unless the awk script is very short, i'd use parameter passing with awk because of all the possible misunderstanding that could be caused by which quotes match up and which variables are expanded by the shell and which are used with awk itself.

your awk example could be done as
avar=5
print "1 2 3 4 5" |
awk "{ print \$$avar; }"