title: Bash script - How to loop through rows in a CSV file
body: <p>I am working with a huge CSV file (<code>filename.csv</code>) that contains a single column. From column 1, I wanted to read current row and compare it with the value of the previous row. If it is <code>greater</code> OR <code>equal</code>, continue comparing and if the value of the current cell is <code>smaller</code> than the previous row - divide the value of the current cell by the value of the previous cell and exit by printing the value of the division. For example from the following example: i wanted my <code>bash</code> script to divide 327 by 340 and print 0.961765 to the console and exit. </p>
  
  <pre><code>338
  338
  339
  340
  327
  301
  299
  284
  284
  283
  283
  283
  282
  282
  282
  283
  </code></pre>
  
  <p>I tried it with the following awk and it works perfectly fine.</p>
  
  <p><code>awk '$1 &lt; val {print $1/val; exit} {val=$1}' filename.csv</code></p>
  
  <p>However, since i want to include around 7 conditional statements (<code>if-else</code>'s), I wanted to do it with a bit cleaner bash script and here is my approach. I am not that used to awk to be honest and that's why i prefer to use bash. </p>
  
  <pre><code>#!/bin/bash
  
  FileName="filename.csv"
  # Test when to stop looping
  STOP=1
  
  # to find the number of columns
  NumCol=`sed 's/[^,]//g' $FileName | wc -c`; let "NumCol+=1"
  
  # Loop until the current cell is less than the count+1
  
  while [ "$STOP" -lt "$NumCol" ]; do
  
          cat $FileName | cut -d, -f$STOP
          let "STOP+=1"
  done
  </code></pre>
  
  <p>How can we loop through the values and add conditional statements? </p>
  
  <p>PS: the criteria for my if-else statement is (if the value (<code>$1/val</code>) is >=0.85 and &lt;=0.9, print A, else if the value (<code>$1/val</code>) is >=0.7 and &lt;=0.8, print B, if the value (<code>$1/val</code>) is >=0.5 and &lt;=0.6 print C otherwise print D).</p>