title: Save contents of those files which contain a specific known string in an single .txt or .tmp file using perl
body: <p>I'm trying to write a perl script where I'm trying to save whole contents of those files which contain a specific string 'PYAG_GENERATED', in a single .txt/.tmp file one after another. These file names are in a specific pattern and this pattern is 'output_nnnn.txt' where nnnn is 0001,0002 and so on. But I don't know how many number of files are present with this 'output_nnnn.txt' name. 
  I'm new in perl and I don't know how I can resolve this issue to get the output correctly. Can anyone help me. Thanks in advance. </p>
  
  <p>I've tried to write perl script in different ways but nothing is coming in output file. I'm giving here one of those I've tried. 'new_1.txt' is the new file where I want to save the expected output and "PYAG_GENERATED" is that specific string I'm finding for in the files.</p>
  
  <pre><code>open(NEW,"&gt;&gt;new_1.txt") or die "could not open:$!";
  $find2="PYAG_GENERATED";
  $n='0001';
  while('output_$n.txt'){
      if(/find2/){
      print NEW;
      }
      $n++;
  }
  close NEW;
  </code></pre>
  
  <p>I expect that the output file 'new_1.txt' will save the whole contents of the the files(with filename pattern 'output_nnnn.txt') which have 'PYAG_GENERATED' string at least once inside.</p>