title: How to increment array dynamically with 'awk'?
body: <p>I have file that contains list of IPs:</p>
  
  <pre><code>1.1.1.1
  2.2.2.2
  3.3.3.3
  5.5.5.5
  1.1.1.1
  5.5.5.5
  </code></pre>
  
  <p>I want to create file that prints list of counters of above mentioned IPs like:</p>
  
  <pre><code>1.1.1.1: 2
  2.2.2.2: 1
  3.3.3.3: 1
  5.5.5.5: 2
  </code></pre>
  
  <p>Where 2,1,1,2 are counters.</p>
  
  <p>I started to write script that work for final count IPs and known count but don't know how to continue</p>
  
  <pre><code> ./ff.sh file_with_IPs.txt
  </code></pre>
  
  <p><strong>script</strong></p>
  
  <pre><code>#!/bin/sh
  
  file=$1
  
  awk  '
  BEGIN {
      for(x=0; x&lt;4; ++x)
          count[x] = 0;
  
   ip[0] = "1.1.1.1";
   ip[1] = "2.2.2.2";
   ip[2] = "3.3.3.3";
   ip[3] = "5.5.5.5";    
  }
  {
      if($1==ip[0]){
         count[0] += 1;
       } else if($1==ip[1]){
         count[1] += 1;
       }else if($1==ip[2]){
         count[2] += 1;
       }else if($1==ip[3]){
         count[3] += 1;
       }
  }
  END {
      for(x=0; x&lt;4; ++x) {
         print ip[x] ": " count[x]
      }
  }
  ' $file &gt; newfile.txt
  </code></pre>
  
  <p>The main problem that I don't know how many IPs stored in file and how they look like.</p>
  
  <p>So I need to increment array <code>ip</code> each time when awk catch new IP.</p>