title: Script to add and remove columns depending on the variable length of the line in a file
body: <p>I have n number of records in a file karan.csv in the following format:</p>
  
  <pre><code>A=9607738162|B=9607562681|C=20200513191434|D=|F=959852599|G=MT|H=4012|I=4012|J=9607562681|K=947100410|
  A=960299773008|B=9607793008|C=20200513191327|D=|E=ST|F=959852599|G=MO|H=2001|I=2001|J=9607793008|K=947100180|
  A=9607704530|B=9607839496|C=20200513191730|D=|F=959852599|G=MT|I=5012|J=9607839496|K=|
  </code></pre>
  
  <p>Now if we notice, the number of columns are: 10, 11 &amp; 9 respectively. This count is random within the file, however, the number of columns will remain the same.</p>
  
  <p>Now, I wan to create a script that will remove $5 from that column (including the delimiter) if there are 11 columns in a line such that it looks exactly like the row with 10 columns</p>
  
  <pre><code>A=9607738162|B=9607562681|C=20200513191434|D=|F=959852599|G=MT|H=4012|I=4012|J=9607562681|K=947100410|
  </code></pre>
  
  <p>and, that adds "H=|" in $7 where the column count is 9 </p>
  
  <pre><code>A=9607704530|B=9607839496|C=20200513191730|D=|F=959852599|G=MT|H=|I=5012|J=9607839496|K=|
  </code></pre>
  
  <p>Now I wrote the following code to achieve it:</p>
  
  <pre><code>for text in $(cat /tmp/karan.csv);do
    count=`awk -F"|" '{print NF-1}' $text`
    if [ $count == 9 ]
    then
    awk  'BEGIN{FS=OFS="|"}{$7="|H"}1' $text &gt;&gt; /tmp/karantest2.csv
    elif [ $count == 10 ]
    then
    echo $text &gt;&gt; /tmp/karantest2.csv
    else
    awk -F"|" '{print $1,$2,$3,$4,$6,$7,$8,$9,$10,$11}' $text &gt;&gt; /tmp/karantest2.csv
    fi
    done
  </code></pre>
  
  <p>But after debugging, I realised the script was not moving ahead after:</p>
  
  <pre><code>count=`awk -F"|" '{print NF-1}' $text`
  </code></pre>
  
  <p>Can any one please me regarding the same.</p>
  
  <p>Regards</p>