 <p>This is my answer: the key point is forcing an integer as the index of the <code>seen</code> array by adding <code>0</code> to $1, that by appropriate use of ls and FS is guaranteed to be an integer.</p> <p>About taking track of the maximum value, the input <em>should</em> be sorted, but who knows...</p> <h3><em>Edit</em></h3> <p><del><code>ls [0-9][0-9][0-9][0-9].* | awk -F. '</code></del></p> <p>To faithfully take into account the OP request, I've modified the 1st line of my code</p> <pre><code>ls [0-9][0-9][0-9][0-9].{png,jpg} | awk -F. ' /./ { seen[$1+0] = 1; m = $1&gt;m ? $1 : m } END { for(i=0;i&lt;m;i++) { printf( seen[i]? "" : "No file named %4.4d.{png,jpg} in here\n",i)}}' </code></pre>