<?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: use variable in awk.. in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840036#M779537</link>
    <description>Another technique to pass shell variables to awk/nawk is to single quote the special awk symbols, then insert the variable in the appropriate location:&lt;BR /&gt; &lt;BR /&gt;SEARCHSTRING="somePattern"&lt;BR /&gt;awk '/'"$SEARCHSTRING"'/ {print $2}' /mypath/myfile&lt;BR /&gt; &lt;BR /&gt;or&lt;BR /&gt; &lt;BR /&gt;MYFIELD=5&lt;BR /&gt;awk '{ print $'$MYFIELD'}'&lt;BR /&gt; &lt;BR /&gt;Just escape the characters needed by awk and insert the variable's value as needed. It's a little simpler than using awk variables on the command line. But if you need the awk variables inside a separate script (not in the command line or in a here document), you can use the previous examples.</description>
    <pubDate>Wed, 09 Aug 2006 11:27:25 GMT</pubDate>
    <dc:creator>Bill Hassell</dc:creator>
    <dc:date>2006-08-09T11:27:25Z</dc:date>
    <item>
      <title>use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840029#M779530</link>
      <description>Hello I have part of my program that uses awk..and calculate value...&lt;BR /&gt;so I get $w  variable with one value..&lt;BR /&gt;&lt;BR /&gt;then in the other part of script I want to use that value in other nawk...&lt;BR /&gt;&lt;BR /&gt;basiclly&lt;BR /&gt;&lt;BR /&gt;other part would be...&lt;BR /&gt;...&lt;BR /&gt;...&lt;BR /&gt;..&lt;BR /&gt;&lt;BR /&gt;q=`cat file20060808 | wc -l`&lt;BR /&gt;echo $q&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;nawk -F"|" -v x=20060808 -v w=$q'&lt;BR /&gt;{u=u+1; l=l+$77}&lt;BR /&gt;($77 != 0)&amp;amp;&amp;amp;($123 == 1)&amp;amp;&amp;amp;($124 == 0) { a=a+1; s=s+$77}  &lt;BR /&gt;END { print x"|"u"|"l"|"q"|"s } ' fileA &amp;gt;&amp;gt; final&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;but this does not pass value of echo $q to my nawk..&lt;BR /&gt;&lt;BR /&gt;do I miss something?&lt;BR /&gt;&lt;BR /&gt;thanks</description>
      <pubDate>Wed, 09 Aug 2006 03:53:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840029#M779530</guid>
      <dc:creator>amonamon</dc:creator>
      <dc:date>2006-08-09T03:53:38Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840030#M779531</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Why is this line, with only one "'"?&lt;BR /&gt;&lt;BR /&gt;nawk -F"|" -v x=20060808 -v w=$q'&lt;BR /&gt;&lt;BR /&gt;Chan&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Aug 2006 04:41:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840030#M779531</guid>
      <dc:creator>Chan 007</dc:creator>
      <dc:date>2006-08-09T04:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840031#M779532</link>
      <description>I was just courious how to pass variable from previous operation to this nawk..so that nawk can print it in its print function...</description>
      <pubDate>Wed, 09 Aug 2006 05:28:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840031#M779532</guid>
      <dc:creator>amonamon</dc:creator>
      <dc:date>2006-08-09T05:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840032#M779533</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;try passing like w=$(echo $q)&lt;BR /&gt;&lt;BR /&gt;Chan</description>
      <pubDate>Wed, 09 Aug 2006 05:43:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840032#M779533</guid>
      <dc:creator>Chan 007</dc:creator>
      <dc:date>2006-08-09T05:43:26Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840033#M779534</link>
      <description>chan...perfecto...that worked...;)</description>
      <pubDate>Wed, 09 Aug 2006 05:51:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840033#M779534</guid>
      <dc:creator>amonamon</dc:creator>
      <dc:date>2006-08-09T05:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840034#M779535</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;using 'nawk' tells me that you probably are working under Solaris...&lt;BR /&gt;&lt;BR /&gt;Nevertheless this should work, since the spaces are stripped from the content of $q:&lt;BR /&gt;&lt;BR /&gt;x=file20060808&lt;BR /&gt;q=`wc -l &amp;lt;$x`&lt;BR /&gt;echo "'$q'"  # blanks contained&lt;BR /&gt;q=`echo $q`  # no blanks contained&lt;BR /&gt;q=`wc -l &amp;lt;$x | tr -d ' '`&lt;BR /&gt;echo "'$q'"  # no blanks contained&lt;BR /&gt;&lt;BR /&gt;nawk -F'|' -v x=$x -v w=$q '...&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Wed, 09 Aug 2006 09:41:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840034#M779535</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-08-09T09:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840035#M779536</link>
      <description>amonamon,&lt;BR /&gt;&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; q=`cat file20060808 | wc -l`&lt;BR /&gt;&lt;BR /&gt;Why create a process, activate 'cat' and pipe all the data to wc when wc is perfectly happy to read the file itself?&lt;BR /&gt;&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; then in the other part of script I want to use that value in other nawk...&lt;BR /&gt;&lt;BR /&gt;The problem has been solved in prior replies, but I'd like you to consider just doing all the work in AWK.&lt;BR /&gt;&lt;BR /&gt;Something like (untested...)&lt;BR /&gt;&lt;BR /&gt;nawk -F"|" -v x=20060808 '&lt;BR /&gt;BEGIN {file = "file" x; while (getline &amp;lt; file ) {w++}}&lt;BR /&gt;{....&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;btw... in your question yo pick up $q, try to assign to awk variable 'w' but then appear to use 'q' in the END for the awk script. Did you not intent to use 'w' in the print, or just use q as the variable for awk as well?&lt;BR /&gt;&lt;BR /&gt;fwiw,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Aug 2006 10:33:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840035#M779536</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-08-09T10:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840036#M779537</link>
      <description>Another technique to pass shell variables to awk/nawk is to single quote the special awk symbols, then insert the variable in the appropriate location:&lt;BR /&gt; &lt;BR /&gt;SEARCHSTRING="somePattern"&lt;BR /&gt;awk '/'"$SEARCHSTRING"'/ {print $2}' /mypath/myfile&lt;BR /&gt; &lt;BR /&gt;or&lt;BR /&gt; &lt;BR /&gt;MYFIELD=5&lt;BR /&gt;awk '{ print $'$MYFIELD'}'&lt;BR /&gt; &lt;BR /&gt;Just escape the characters needed by awk and insert the variable's value as needed. It's a little simpler than using awk variables on the command line. But if you need the awk variables inside a separate script (not in the command line or in a here document), you can use the previous examples.</description>
      <pubDate>Wed, 09 Aug 2006 11:27:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840036#M779537</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2006-08-09T11:27:25Z</dc:date>
    </item>
    <item>
      <title>Re: use variable in awk..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840037#M779538</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Most modern versions of 'awk' support the '-v' option for passing variables.  Older versions insist the the external assignment is made as part of the argument list passed.  Compare:&lt;BR /&gt;&lt;BR /&gt;# awk -v VAR="world" 'BEGIN{print "hello",VAR}' /dev/null &lt;BR /&gt;&lt;BR /&gt;# awk -v VAR="world" 'END{print "hello",VAR}' /dev/null&lt;BR /&gt;&lt;BR /&gt;# awk 'BEGIN{print "hello",VAR}' VAR="world" /dev/null&lt;BR /&gt;&lt;BR /&gt;# awk 'END{print "hello",VAR}' VAR="world" /dev/null&lt;BR /&gt;&lt;BR /&gt;Note particularly the diffence in output in the third form.  The advanage to the '-v arg=value' form is that variable assignment occurs even before the 'BEGIN' rules are executed.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 09 Aug 2006 12:19:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/use-variable-in-awk/m-p/3840037#M779538</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-08-09T12:19:30Z</dc:date>
    </item>
  </channel>
</rss>

