<?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 Using awk and variables in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964441#M292136</link>
    <description>Hi all, could you tell me how can i put a variable into awk sentence?&lt;BR /&gt;I mean, below you can see the script and &lt;BR /&gt;rule says that i have to use a string for 9 filed:&lt;BR /&gt;ls -l | awk '$9 == "file.name" { sum += $5 } END { print sum }'&lt;BR /&gt;&lt;BR /&gt;but in this case, i do not know the name, script will know that name through "for" command, but where it says "?????" i do not know how to put "x" variable and not the string ...&lt;BR /&gt;&lt;BR /&gt;THIS IS THE SCRIPT&lt;BR /&gt;&lt;BR /&gt;for x in `ls -1 /oracle/log/ | grep .trc`&lt;BR /&gt;do&lt;BR /&gt;echo ------------------ $x&lt;BR /&gt;ls -l | awk '$9 == ????? { sum += $5 } END { print sum }'&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Please let me know ...&lt;BR /&gt;Thanks !!!  :0)</description>
    <pubDate>Mon, 19 Mar 2007 10:20:55 GMT</pubDate>
    <dc:creator>Manuales</dc:creator>
    <dc:date>2007-03-19T10:20:55Z</dc:date>
    <item>
      <title>Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964441#M292136</link>
      <description>Hi all, could you tell me how can i put a variable into awk sentence?&lt;BR /&gt;I mean, below you can see the script and &lt;BR /&gt;rule says that i have to use a string for 9 filed:&lt;BR /&gt;ls -l | awk '$9 == "file.name" { sum += $5 } END { print sum }'&lt;BR /&gt;&lt;BR /&gt;but in this case, i do not know the name, script will know that name through "for" command, but where it says "?????" i do not know how to put "x" variable and not the string ...&lt;BR /&gt;&lt;BR /&gt;THIS IS THE SCRIPT&lt;BR /&gt;&lt;BR /&gt;for x in `ls -1 /oracle/log/ | grep .trc`&lt;BR /&gt;do&lt;BR /&gt;echo ------------------ $x&lt;BR /&gt;ls -l | awk '$9 == ????? { sum += $5 } END { print sum }'&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Please let me know ...&lt;BR /&gt;Thanks !!!  :0)</description>
      <pubDate>Mon, 19 Mar 2007 10:20:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964441#M292136</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T10:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964442#M292137</link>
      <description>Here are some threads that may help:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1049976" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1049976&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=196334" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=196334&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=324808" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=324808&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Mar 2007 10:26:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964442#M292137</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2007-03-19T10:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964443#M292138</link>
      <description>I will answer your specific question although I am all but certain you aren't asking the correct question. The key to what you are trying to do is the -v option of awk.&lt;BR /&gt;&lt;BR /&gt;ls -l | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Mar 2007 10:29:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964443#M292138</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-03-19T10:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964444#M292139</link>
      <description>thanks for links ..&lt;BR /&gt;&lt;BR /&gt;Clay .. i run script but it did not work :'(  &lt;BR /&gt;&lt;BR /&gt;this is the script:&lt;BR /&gt;for x in `ls -1 /oracle/log/ | grep .trc`&lt;BR /&gt;do&lt;BR /&gt;echo ------------------ $x&lt;BR /&gt;ls -l | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;this is the output &lt;BR /&gt;&lt;BR /&gt;------------------ ora_17362_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_17385_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_17402_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_17419_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_17450_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_17961_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_18221_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_18233_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_18244_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_18291_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_18297_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_18722_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_19000_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_19018_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_19030_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_19110_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_19126_p09.trc&lt;BR /&gt;&lt;BR /&gt;------------------ ora_19511_p09.trc &lt;BR /&gt;&lt;BR /&gt;i mean, it did not work because i can not see the size growing ...&lt;BR /&gt;how can i do that?&lt;BR /&gt;&lt;BR /&gt;Thanks.</description>
      <pubDate>Mon, 19 Mar 2007 10:55:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964444#M292139</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T10:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964445#M292140</link>
      <description>Hi Manuales:&lt;BR /&gt;&lt;BR /&gt;The '-v' option for passing variables is the ideal method.  Another variation you may encounter is some scripts is seen with older 'awk' versions that insist that the external assignment is made as part of the argument list passed.  Compare these cases:&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 the difference in output in the third form [where only the word "hello" is output].  The advanage to the '-v arg=value' form is that variable assignment occurs *before* the 'BEGIN' rules are executed.  To enable the third case to offer "hello world" we would have to change the 'BEGIN' to an 'END'.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 19 Mar 2007 10:55:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964445#M292140</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-03-19T10:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964446#M292141</link>
      <description>it do not work  :'(</description>
      <pubDate>Mon, 19 Mar 2007 10:59:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964446#M292141</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T10:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964447#M292142</link>
      <description>As I said, you aren't asking the right question -- but you are getting closer to the right question. &lt;BR /&gt;&lt;BR /&gt;I will give you a hint. Your sum is only summing one file within a single awk invocation and you need a single awk invocation with multiple lines of input.&lt;BR /&gt;&lt;BR /&gt;This will be closer (but still needs a little work):&lt;BR /&gt;&lt;BR /&gt;Create (or build on the fly an awk script), my.awk:&lt;BR /&gt;--------------------------------&lt;BR /&gt;BEGIN {&lt;BR /&gt;  sum = 0&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;  sum += ($5 + 0)&lt;BR /&gt;}&lt;BR /&gt;END {&lt;BR /&gt;  print sum&lt;BR /&gt;}&lt;BR /&gt;--------------------------------&lt;BR /&gt;&lt;BR /&gt;Now:&lt;BR /&gt;ls -l /oracle/log/*.trc | awk -f my.awk&lt;BR /&gt;&lt;BR /&gt;Note: The ($5 + 0) seems dumb but it is not and is intentional.</description>
      <pubDate>Mon, 19 Mar 2007 11:06:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964447#M292142</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-03-19T11:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964448#M292143</link>
      <description>Clay, you are right ..&lt;BR /&gt;script works as follows, but only shows the size of every file  :(  and i need to adding every size of each one ..&lt;BR /&gt;&lt;BR /&gt;for x in `ls -1 /oracle/log/ | grep .trc`&lt;BR /&gt;do&lt;BR /&gt;ls -l /oracle/log/ | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'&lt;BR /&gt;done&lt;BR /&gt;------------------ ora_17362_p09.trc&lt;BR /&gt;21667&lt;BR /&gt;------------------ ora_17385_p09.trc&lt;BR /&gt;21667&lt;BR /&gt;------------------ ora_17402_p09.trc&lt;BR /&gt;21667&lt;BR /&gt;&lt;BR /&gt;OK, I WILL DO THE FOLLOWING, but ... is possible to put the following code into the script, i mean , in a line?&lt;BR /&gt;--------------------------------&lt;BR /&gt;BEGIN {&lt;BR /&gt;sum = 0&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;sum += ($5 + 0)&lt;BR /&gt;}&lt;BR /&gt;END {&lt;BR /&gt;print sum&lt;BR /&gt;}&lt;BR /&gt;--------------------------------&lt;BR /&gt;&lt;BR /&gt;Please , let me know ..&lt;BR /&gt;Thanks.</description>
      <pubDate>Mon, 19 Mar 2007 11:12:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964448#M292143</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T11:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964449#M292144</link>
      <description>okok ... Clay, it worked , but i'd like to know if that code ("BEGIN....") can be written into the main script, for having only one script.&lt;BR /&gt;&lt;BR /&gt;Thanks.&lt;BR /&gt;Manuales.</description>
      <pubDate>Mon, 19 Mar 2007 11:19:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964449#M292144</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T11:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964450#M292145</link>
      <description>Hi Manuales:&lt;BR /&gt;&lt;BR /&gt;# ls -l /oracle/log | awk '$9~/\.trc/ {print;sum+=$5} END {print sum}'&lt;BR /&gt;&lt;BR /&gt;Notice how we let 'awk' match the fields to sum.  You don't need to 'grep' first!&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 19 Mar 2007 11:21:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964450#M292145</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-03-19T11:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964451#M292146</link>
      <description>THANKS A LOT !!!!!!!!&lt;BR /&gt;AND if i do not want to be shown the files then only i added last command  (grep -v ora, i know that files beging with ora word):&lt;BR /&gt;&lt;BR /&gt;ls -l /oracle/log/ | awk '$9~/\.trc/ {print;sum+=$5} END {print sum}' | grep -v ora&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;THANKS TO ALL ...&lt;BR /&gt;AND .. do you know a manual or tutorial on line on internet for studying more awk commnad?&lt;BR /&gt;&lt;BR /&gt;Thanks.&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Mar 2007 11:28:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964451#M292146</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T11:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964452#M292147</link>
      <description>a last question ..&lt;BR /&gt;how do you that:&lt;BR /&gt;ls -l /oracle/log/ | awk '$9~/\.trc/ {print;sum+=$5} END {print sum}' | grep -v ora&lt;BR /&gt;&lt;BR /&gt;if instead of being files they were folders with files????? i mean, how do i put a command for being recursive?&lt;BR /&gt;&lt;BR /&gt;Thanks ..</description>
      <pubDate>Mon, 19 Mar 2007 11:30:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964452#M292147</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T11:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964453#M292148</link>
      <description>Yes, it is possible. However, it is much easier to get awk working exactly as you want it to as a separate file before worrying about building it on the fly or quoting it within a script.&lt;BR /&gt;&lt;BR /&gt;So take that approach and finally I'll show you one technique for building the awk script on the fly. &lt;BR /&gt;&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;&lt;BR /&gt;build_A1()&lt;BR /&gt;{&lt;BR /&gt;  echo "BEGIN {"      &lt;BR /&gt;  echo "  sum = 0"&lt;BR /&gt;  echo "}"&lt;BR /&gt;  echo "{"&lt;BR /&gt;  echo "  print \$NF"&lt;BR /&gt;  echo "  sum += (\$5 + 0)"&lt;BR /&gt;  echo "}"&lt;BR /&gt;  echo "END {"&lt;BR /&gt;  echo "print sum"&lt;BR /&gt;  echo "}"&lt;BR /&gt;  return 0&lt;BR /&gt;} # build_A1&lt;BR /&gt;&lt;BR /&gt;typeset A1=$(build_A1)&lt;BR /&gt;awk "${A1}" &lt;BR /&gt;---------------------------------------&lt;BR /&gt;You then invoke it as:&lt;BR /&gt;ls -l /oracle/log/*.trc | my.sh&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Mar 2007 11:30:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964453#M292148</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-03-19T11:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964454#M292149</link>
      <description>Hi (again) Manuales:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; do you know a manual or tutorial on line on internet for studying more awk commnad?&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://www.gnu.org/software/gawk/manual/" target="_blank"&gt;http://www.gnu.org/software/gawk/manual/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 19 Mar 2007 11:31:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964454#M292149</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-03-19T11:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964455#M292150</link>
      <description>Hi (again!) Manuales:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; if instead of being files they were folders with files????? i mean, how do i put a command for being recursive?&lt;BR /&gt;&lt;BR /&gt;Simply use 'find' and pipe its output to 'awk':&lt;BR /&gt;&lt;BR /&gt;# find /oracle/log -type f -name "*.trc" -exec ls -l {} \;|awk '{print;sum+=$5};END {print sum}'&lt;BR /&gt;&lt;BR /&gt;Notice that we let 'find' do the matching of filenames and execute the 'ls'.  The 'awk' portion does the summation, only, now.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Mar 2007 11:36:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964455#M292150</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-03-19T11:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using awk and variables</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964456#M292151</link>
      <description>You are right James ....&lt;BR /&gt;&lt;BR /&gt;THANKS TO ALL!!!!!&lt;BR /&gt;THANKS FOR YOUR HELP !!!&lt;BR /&gt;&lt;BR /&gt;Talk you later, in my next doubt .&lt;BR /&gt;:D</description>
      <pubDate>Mon, 19 Mar 2007 11:42:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-awk-and-variables/m-p/3964456#M292151</guid>
      <dc:creator>Manuales</dc:creator>
      <dc:date>2007-03-19T11:42:53Z</dc:date>
    </item>
  </channel>
</rss>

