title: How to add a column to a line in a cvs file for later processing in a bash script
body: <p>I have a bash script that is processing orders that are stored in a CSV file to generate product documentation to be emailed to the customers at a later time. My problem is that my script is currently emailing each of the products out as they are generated instead of storing each customer's full order in one line of a CSV file for processing by an email script after I have uploaded the upload CSV file.</p>
  
  <p>I have tried using array(), awk, seg but those are not working the way I want.</p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  csvFile=$wdir/$1
  csvfileup=$wdir/product/upload_$yr-$mo-$dy_$hr-$mn-$sc.csv
  # create header in upload cvs file
  echo "name;sku;description;category_1;category_2;category_3;image_url;weight;price;compair_to_price;quantity;enabled;UPC;brand;page_title;meta_description" &gt;&gt; $csvfileup
  #function to generate files based off of information retrieved from a csv file
  fun_genfiles(){
          cd $wdir/product/$3
          max=$(($countl + $1))
          while [ $count -lt $max ]; do
              count=$((++count))
              nval=&lt;serial number generation&gt;
              cp $wdir/$srcfile $wdir/product/$3/$2$nval.pdf
              echo $3/$2$nval
              echo &lt;product information&gt; &gt;&gt; $csvfileup
              #mailx -A $2$nval.pdf --content-type=text/html -s "Here is non-registered copy $count of the product." --return-address=&lt;return@medomain.com&gt; &lt; ~/Downloads/commandlinesending/mail.txt $4
          done
          countl=$max
          #echo $countl
      }
  #read Cvs File and call fun_genfiles with the proper values according to the type of order placed
  
  while read -r line
  do
      field1=$(echo $line | awk -F',' '{printf "%s", $3}' | tr -d '"')
      field2=$(echo $line | awk -F',' '{printf "%s", $16}' | tr -d '"')
      field3=$(echo $line | awk -F',' '{printf "%s", $15}' | tr -d '"')
      field4=$(echo $line | awk -F',' '{printf "%s", $2}' | tr -d '"')
      echo $field1 $field2 $field3 $field4
  # Generate 
      if [ $field1 = "Product 1" ]; then
          mkdir $wdir/product/$field2
          cp $wdir/$srcfile $wdir/product/$field2/$field2.pdf
          echo &lt;master copy information&gt; &gt;&gt; $csvfileup
          #cd $wdir/product/$field2
          #mailx -A $field2.pdf --content-type=text/html -s "Here is your personal copy of Product 1." --return-address=&lt;return@medomain.com&gt; &lt; ~/Downloads/commandlinesending/mail.txt $field4
          fun_genfiles 21 "PD1" $field2 $field4 $field3
      elif [ $field1 = "Product 2" ]; then
          mkdir $wdir/product/$field2
          cp $wdir/$srcfile $wdir/product/$field2/$field2.pdf
          echo &lt;master copy information&gt; &gt;&gt; $csvfileup
          #cd $wdir/product/$field2
          #mailx -A $field2.pdf --content-type=text/html -s "Here is your personal copy of Product 2." --return-address=&lt;return@medomain.com&gt; &lt; ~/Downloads/commandlinesending/mail.txt $field4
          fun_genfiles 21 "PD2" $field2 $field4 $field3
      elif [ $field1 = "Product 3" ]; then
          mkdir $wdir/product/$field2
          cp $wdir/$srcfile $wdir/product/$field2/$field2.pdf
          echo &lt;master copy information&gt; &gt;&gt; $csvfileup
          #cd $wdir/product/$field2
          #mailx -A $field2.pdf --content-type=text/html -s "Here is your personal copy of Product 3." --return-address=&lt;return@medomain.com&gt; &lt; ~/Downloads/commandlinesending/mail.txt $field4
          fun_genfiles 21 "PD3" $field2 $field4 $field3
      fi
  done &lt; $csvFile
  </code></pre>
  
  <p>What I am expecting is that the email.csv file that is generated as a single line for each order with the first column being the email address and columns 2 - product count is each serial number of the order. With what I have had for output is the email CSV file has what should be on one line for each order is actually on multiple lines instead.</p>
  
  <p>Edit : </p>
  
  <p>The mailx and sleep lines will be deleted as those lines are going into a different script and they are the only thing that slows the processing down.</p>
  
  <p>Here is the header line and a couple of sample lines of the downloaded order.csv:</p>
  
  <pre><code>order_number,email,SKU,name,&lt;....&gt;,payment_method,&lt;....&gt;,customer_ipaddress,&lt;....&gt;
  00110019,foo_bar@foobar.com,Foo Bar,&lt;....&gt;,VISA,&lt;....&gt;,999.999.99.99
  00110019,foo_bar2@foobar.com,Foo2 Bar,&lt;....&gt;,VISA,&lt;....&gt;,999.998.99.99
  00110019,foo_bar3@foobar.com,Foo3 Bar,&lt;....&gt;,VISA,&lt;....&gt;,999.999.98.99
  </code></pre>
  
  <p>Now after less than a second of script run time I have a properly formatted and ready to upload product verification csv file so that par is working just fine. My problem is that I'm wanting to have another script that I will work on later that handles sending out the emails. What I'm trying to do is have this functioning script write another CSV file that is for the email script to use.</p>
  
  <p>The expected output for the email.csv file is like this:</p>
  
  <pre><code>Foo Bar,foo_bar@foobar.com,masterSN,copy1sn,&lt;....&gt;,copy21sn
  </code></pre>
  
  <p>But the output I get is like below:</p>
  
  <pre><code>Foo Bar,foo_bar@foobar.com,masterSN
  copy1SN
  &lt;...&gt;
  copy12SN
  </code></pre>
  
  <p>and so forth for each order.</p>
  
  <p>I apologize for misunderstanding how the people that have replied meant for me to respond and I do thank them for their responses.</p>