title: replace nth occurrence of character in a file using awk regardless of the line
body: <p>I am trying to replace the nth occurrence of a character or string regardless of the line using awk.</p>
  
  <p>So if our data was this</p>
  
  <pre><code>|||||||
  ||||||
  |||||
  |||
  </code></pre>
  
  <p>and we were trying to replace | with A</p>
  
  <p>then the output should look like this, assuming we want to replace every 3rd occurance</p>
  
  <pre><code>||A||A|
  |A||A|
  |A||A
  ||A
  </code></pre>
  
  <p>The current awk command I am using is this</p>
  
  <pre><code> awk '/|/{c++;if(c==3){sub(/|/,"A");c=0}}1' test.data
  </code></pre>
  
  <p>and it wrongly outputs this</p>
  
  <pre><code>|||||||
  ||||||
  A||||
  |||
  </code></pre>
  
  <p>also the data can look like this</p>
  
  <pre><code>|||xfsafrwe|||asfasdf|
  |safasf|||asfasdf||
  ||asfasf|||
  |||
  </code></pre>
  
  <p>and the result of course is this</p>
  
  <pre><code>||Axfsafrwe||Aasfasdf|
  |safasfA||asfasdfA|
  |Aasfasf||A
  ||A
  </code></pre>
  
  <p>Thanks</p>