title: Using awk to put a header in a text file
body: <p>I have lots of text files and need to put a header on each one of them depending of the data on each file. </p>
  
  <p>This awk command accomplishes the task:</p>
  
  <pre><code>awk 'NR==1{first=$1}{sum+=$1;}END{last=$1;print NR,last,"L";}' my_text.file
  </code></pre>
  
  <p>But this prints it on the screen and I want to put this output in the header of each of my file, and saving the modifications with the same file name.</p>
  
  <p>Here is what I've tried:</p>
  
  <pre><code>for i in *.txt
  do
  echo Processing ${i}
  cat awk 'NR==1{first=$1}{sum+=$1;}END{last=$1;print NR,last,"L";}' "${i}" ${i} &gt; $$.tmp &amp;&amp; mv $$.tmp "${i}"
  done
  </code></pre>
  
  <p>So I guess I can't use cat to put them as a header, or am I doing something wrong?</p>
  
  <p>Thanks in advance</p>