Unix: How can I count all lines containing a string in all files in a directory and see the output for each file separately
<p>In UNIX I can do the following:</p>
<pre><code>grep -o 'string' myFile.txt | wc -l
</code></pre>
<p>which will count the number of lines in myFile.txt containing the string.
Or I can use :</p>
<pre><code>grep -o 'string' *.txt | wc -l
</code></pre>
<p>which will count the number of lines in all .txt extension files in my folder containing the string.
I am looking for a way to do the count for all files in the folder but to see the output separated for each file, something like:</p>
<blockquote>
<p>myFile.txt 10000</p>
<p>myFile2.txt 20000</p>
<p>myFile3.txt 30000</p>
</blockquote>
<p>I hope I have made my self clear, if not you can see a somewhat close example in the output of :</p>
<pre><code>wc -l *.txt
</code></pre>