How to divide my script output by the output of another command?
<p>I have a folder, <code>my_folder</code>, which contains over 800 files named <code>myfile_*.dat</code> where * is the unique ID for each file. In my file I basically have a variety of repeated fields but the one I am interested in is the <code>&lt;rating&gt;</code> field. Lines of this field look like the following: <code>&lt;rating&gt;n</code> where n is the rating score. I have a script which sums up all of the ratings per file, but now I must divide it by the number of lines that have <code>&lt;rating&gt;n</code> in order to obtain an average rating per file. Here is my script:</p>

<pre><code>dir=$1
cd $dir
grep -P -o '(?&lt;=&lt;rating&gt;).*' * |awk -F: '{A[$1]+=$2;next}END{for(i in A){print i,A[i]}}'|sort -nr -k2
</code></pre>

<p>I figure that I would use <code>grep -c &lt;rating&gt; myfile_*.dat</code> to count the number of matching lines and then divide the sum by this count per file but do not know where to put this in my script? Any suggestions are appreciated.</p>

<p>My script takes the folder name as an argument in the command line.</p>

<p><strong>INPUT FILE</strong></p>

<pre><code>&lt;Overall Rating&gt;
&lt;Avg. Price&gt;$155
&lt;URL&gt;

&lt;Author&gt;Jeter5
&lt;Content&gt;I hope we're not disappointed! We enjoyed New Orleans...
&lt;Date&gt;Dec 19, 2008
&lt;No. Reader&gt;-1
&lt;No. Helpful&gt;-1
&lt;rating&gt;4
&lt;Value&gt;-1
&lt;Rooms&gt;3
&lt;Location&gt;5
&lt;Cleanliness&gt;3
&lt;Check in / front desk&gt;5
&lt;Service&gt;5
&lt;Business service&gt;5

&lt;Author&gt;...
repeat fields again...
</code></pre>