title: Batch create files with name and content based on input file
body: <p>I am a mac OS user trying to batch create a bunch of files. I have a text file with column of several hundred terms/subjects, eg:</p>
  
  <pre><code>hydrogen
  oxygen
  nitrogen
  carbon
  etcetera
  </code></pre>
  
  <p>I want to programmatically fill a directory with text files generated from this subject list. For example, "hydrogen.txt" and "oxygen.txt" and so on, with each file created by iterating through the lines of my list_of_names.txt file. Some lines are one word, but other lines are two or three words (eg: "carbon monoxide"). This I have figured out how to do:</p>
  
  <pre><code>awk 'NF&gt;0' list_of_names.txt | while read line; do touch "${line}.txt"; done
  </code></pre>
  
  <p>Additionally I need to create two lines of content within each of these files, and the content is both static and dynamic...</p>
  
  <pre><code># filename
  #elements/filename
  </code></pre>
  
  <p>...where in the example above the pound sign ("#") and "elements/" would be the same in all of the files created, but "filename" would be variable (eg: "hydrogen" for "hydrogen.txt" and "oxygen" for "oxygen.txt" etc). One further wrinkle is that if any spaces appear at all on the second line of content, there needs to be a trailing pound sign. For example:</p>
  
  <pre><code># filename
  #elements/carbon monoxide#
  </code></pre>
  
  <p>...although this last part is not a dealbreaker and I can use grep to modify list_of_names.txt such that phrases like "carbon monoxide" become "carbon_monoxide" and just deal with the repercussions of this later. (But if it is easy to preserve the spaces, I would prefer that.)</p>
  
  <p>After a couple hours of searching and attempts to use sed, awk, and so on I am stuck at a directory full of files with the correct filename.txt format, but I can't get further that this. Mostly I think my efforts are failing because the solutions I can find for doing something like this are using commands I am not familiar with and they are structured for GNU and don't execute correctly in Terminal on Mac OS.</p>
  
  <p>I am amenable to processing this in multiple steps (ie make all of the files.txt first, then run a second step to populate the content of the files), or as a single command that makes the files and all of their content simultaneously ('simultaneously' from a human timescale).</p>
  
  <p>My horrible pseudocode (IN CAPS) for how this would look as 2 steps:</p>
  
  <pre><code>awk 'NF&gt;0' list_of_names.txt | while read line; do touch "${line}.txt"; done
  awk 'NF&gt;0' list_of_names.txt | while read line; OPEN "${line}.txt" AND PRINT "# ${line}\n#elements/${line}"; IF ${line} CONTAINS CHARACTER " " PRINT "#"; done
  </code></pre>