title: Awk - how to print variable with string
body: <p>I have this input and I would like to learn how to save string after = to variable and use it for output filename and in the first line of output that will start with "#"</p>
  
  <pre><code>c ROIysiz= 28
  c column1= HJD
  c RedNumDa= 18262
  c column3= ERROR
  c column2= FLUX
  c end header ---------------------------------------------------------------------------------------
  2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
  2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
  2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
  2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
  2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
  2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
  2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
  2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
  </code></pre>
  
  <p>Desired output, for instance:</p>
  
  <p>name:
  FLUX28ERROR</p>
  
  <p>content:</p>
  
  <pre><code># FLUX 18262
  2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
  2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
  2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
  2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
  2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
  2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
  2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
  2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
  </code></pre>
  
  <p>I have a script:</p>
  
  <pre><code>awk '
  /ROIysiz/{
  second_out=$NF
  }
  /column2/{
  close(out_file)
  found=count=""
  out_file=$NF second_out 
  next
  }
  /end header/{
  found=1
  next
  }
  found &amp;&amp; out_file{
  if(++count==1){
  print "#" $0 &gt; (out_file)
  }
  else{
  print &gt; (out_file)
  }
  }
  ' inputname
  </code></pre>
  
  <p>That gives a file FLUX28:</p>
  
  <pre><code>#2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
  2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
  2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
  2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
  2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
  2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
  2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
  2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
  </code></pre>
  
  <p>I would like to know how to add another string to filename and how to print the first line of the output:</p>
  
  <pre><code># FLUX 18262
  </code></pre>
  
  <p>I tried it by:</p>
  
  <pre><code>awk '
  /ROIysiz/{
  second_out=$NF
  }
  /column 3/{
  third_part=$NF
  }
  /column2/{
  close(out_file)
  found=count=""
  out_file=$NF second_out third_part
  next
  }
  /end header/{
  found=1
  next
  }
  found &amp;&amp; out_file{
  if(++count==1){
  print "#" second_out third_part &gt; (out_file)
  }
  else{
  print &gt; (out_file)
  }
  }
  ' inputname
  </code></pre>
  
  <p>that gives output filename FLUX28 and contains:</p>
  
  <pre><code>#28
  2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
  2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
  2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
  2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
  2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
  2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
  2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
  2.458377252462999895e+06 2.159254025049928832e-02 2.315911471112144012e-03
  2.458377462405352853e+06 1.721511461149537181e-02 1.687658552459528729e-03
  2.458377602279778104e+06 1.744415665326638776e-02 3.041609691486800784e-03
  2.458377956590285990e+06 8.597543276201942419e-03 3.490433838852374532e-03
  2.458378025015166495e+06 6.127180820289755692e-03 2.437530774283428858e-03
  </code></pre>
  
  <p>Why the variable third_part is not printed in output filename and on the first row? Thank you</p>