<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: question on awk and sed in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106776#M147993</link>
    <description>Best way to use shell variable in awk&lt;BR /&gt;is to make it awk variable:&lt;BR /&gt;&lt;BR /&gt;ABC="xyz"&lt;BR /&gt;&lt;BR /&gt;awk -v abc=${ABC} '{print abc}'&lt;BR /&gt;&lt;BR /&gt;In sed just use " instead of '&lt;BR /&gt;&lt;BR /&gt;Kind regards,&lt;BR /&gt;&lt;BR /&gt;Hrvoje</description>
    <pubDate>Fri, 31 Oct 2003 03:25:28 GMT</pubDate>
    <dc:creator>Hrvoje Dubravica</dc:creator>
    <dc:date>2003-10-31T03:25:28Z</dc:date>
    <item>
      <title>question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106766#M147983</link>
      <description>is it possible to use $variable inside sed and awk:&lt;BR /&gt;svar="xyz"&lt;BR /&gt;sed -e 's/$tvar/x/'&lt;BR /&gt;&lt;BR /&gt;avar=5&lt;BR /&gt;awk '{ print $'$avar' }'&lt;BR /&gt;&lt;BR /&gt;Thanks,Charles</description>
      <pubDate>Thu, 30 Oct 2003 13:14:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106766#M147983</guid>
      <dc:creator>Charles Li_2</dc:creator>
      <dc:date>2003-10-30T13:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106767#M147984</link>
      <description>For sure yes with sed.&lt;BR /&gt;&lt;BR /&gt;You may need to use the structure ${tvar} first.&lt;BR /&gt;&lt;BR /&gt;Why don't you just try it.&lt;BR /&gt;&lt;BR /&gt;SEP</description>
      <pubDate>Thu, 30 Oct 2003 13:18:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106767#M147984</guid>
      <dc:creator>Steven E. Protter</dc:creator>
      <dc:date>2003-10-30T13:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106768#M147985</link>
      <description>In awk the answer is yes.&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;BEGIN{&lt;BR /&gt;X=50&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;printf("%d\n",X)&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Will print 50.  &lt;BR /&gt;&lt;BR /&gt;To pass variables from the shell into awk, use:&lt;BR /&gt;&lt;BR /&gt;awk -v var=value&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Oct 2003 13:20:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106768#M147985</guid>
      <dc:creator>Tom Danzig</dc:creator>
      <dc:date>2003-10-30T13:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106769#M147986</link>
      <description>I would have, but I did not know the syntax.  Should it be {},[],(),"",'' or ``.</description>
      <pubDate>Thu, 30 Oct 2003 13:21:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106769#M147986</guid>
      <dc:creator>Charles Li_2</dc:creator>
      <dc:date>2003-10-30T13:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106770#M147987</link>
      <description>As Steven says, use curly braces arount the variable name to isolate it.  i.e ${varname}</description>
      <pubDate>Thu, 30 Oct 2003 13:23:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106770#M147987</guid>
      <dc:creator>Tom Danzig</dc:creator>
      <dc:date>2003-10-30T13:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106771#M147988</link>
      <description>Charles,&lt;BR /&gt;&lt;BR /&gt;1. Enclose the regex within double quotes, and not single quotes.&lt;BR /&gt;sed -e "s/$tvar/x/" ...&lt;BR /&gt;&lt;BR /&gt;2. Works fine for me.&lt;BR /&gt;awk '{ print $'$avar' }'&lt;BR /&gt;this works too -&lt;BR /&gt;awk "{ print \$$avar ;}" ...&lt;BR /&gt;&lt;BR /&gt;- ramd.</description>
      <pubDate>Thu, 30 Oct 2003 13:28:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106771#M147988</guid>
      <dc:creator>Ramkumar Devanathan</dc:creator>
      <dc:date>2003-10-30T13:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106772#M147989</link>
      <description>Tom,&lt;BR /&gt;I want to do the following with awk:&lt;BR /&gt;&lt;BR /&gt;for loop&lt;BR /&gt;K=awk '{ print $$loop }'  &lt;BR /&gt;## giving K a different value each time the loop is executed. &lt;BR /&gt;endloop&lt;BR /&gt;&lt;BR /&gt;is this possible?&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Oct 2003 13:28:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106772#M147989</guid>
      <dc:creator>Charles Li_2</dc:creator>
      <dc:date>2003-10-30T13:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106773#M147990</link>
      <description>Works great.  Thanks to all.</description>
      <pubDate>Thu, 30 Oct 2003 13:35:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106773#M147990</guid>
      <dc:creator>Charles Li_2</dc:creator>
      <dc:date>2003-10-30T13:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106774#M147991</link>
      <description>Charls,&lt;BR /&gt;&lt;BR /&gt;It should be.  Remember not to preceed variables in the awk portion with a $.  The shell uses the $ but not awk.</description>
      <pubDate>Thu, 30 Oct 2003 13:35:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106774#M147991</guid>
      <dc:creator>Tom Danzig</dc:creator>
      <dc:date>2003-10-30T13:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106775#M147992</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;abc="abc";sed -e 's/$abc/def/'&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;abc="abc";sed -e "s/$abc/def/"&lt;BR /&gt;abc="abc";sed -e 's/'$abc'/def/'&lt;BR /&gt;once again sed isn't using the variable, the shell is expanding the variable before sed sees it.&lt;BR /&gt;abc="abc";sed -e "s/$abc/def/" gets expanded by the shell to&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;your awk example could be done as&lt;BR /&gt;avar=5&lt;BR /&gt;print "1 2 3 4 5" |&lt;BR /&gt;awk "{ print \$$avar; }"&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Oct 2003 13:39:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106775#M147992</guid>
      <dc:creator>curt larson_1</dc:creator>
      <dc:date>2003-10-30T13:39:21Z</dc:date>
    </item>
    <item>
      <title>Re: question on awk and sed</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106776#M147993</link>
      <description>Best way to use shell variable in awk&lt;BR /&gt;is to make it awk variable:&lt;BR /&gt;&lt;BR /&gt;ABC="xyz"&lt;BR /&gt;&lt;BR /&gt;awk -v abc=${ABC} '{print abc}'&lt;BR /&gt;&lt;BR /&gt;In sed just use " instead of '&lt;BR /&gt;&lt;BR /&gt;Kind regards,&lt;BR /&gt;&lt;BR /&gt;Hrvoje</description>
      <pubDate>Fri, 31 Oct 2003 03:25:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/question-on-awk-and-sed/m-p/3106776#M147993</guid>
      <dc:creator>Hrvoje Dubravica</dc:creator>
      <dc:date>2003-10-31T03:25:28Z</dc:date>
    </item>
  </channel>
</rss>

