title: Can I use awk within awk in UNIX?
body: <p>I have two files. One contains students and their average grade from the first semester and the second contains students and 5 grades from the second semester. I have to update the first file, so that it contains the students sorted after the yearly average grade. I'm new to unix and so far I've tried to use the awk command within an awk but I keep getting a syntax error. I have tried other ways but all I can manage to achieve is separating the file contents into lines, without direct access to the second word(the average grade) from each line.</p>
  
  <p>input files:</p>
  
  <p>firstsem:</p>
  
  <pre class="lang-none prettyprint-override"><code>Name1 6
  Name2 8
  Name3 10
  </code></pre>
  
  <p>secondsem:</p>
  
  <pre class="lang-none prettyprint-override"><code>Name1 5 6 5 6 5
  Name2 10 10 10 10 10
  Name3 2 2 2 2 2
  </code></pre>
  
  <p>result:</p>
  
  <p>firstsem:</p>
  
  <pre class="lang-none prettyprint-override"><code>Name1  5.7
  Name3 6
  Name2 9
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>awk '{
          name = $1
          total=$2+$3+$4+$5+$6;
          avg=total/5;
          print name " " avg
          #sprintf("",nume,avg)
  }' &lt; secondsem &gt; secondsemesteravg
  # i didn't really want the secondsemesteravg file, but i didn't find another way
  while read line; do
         # wanted to get the avg from firstsem
          echo $line | awk '{$2=avgfirstsem + avgsecondsem/2; print $0;}';
  done &lt; firstsem
  </code></pre>