title: How to get count of unique IP addresses and errors in shell scripting using uniq or awk?
body: <p>I am doing a nslookup on URLs for multiple iterations using shell script. I need to check how many times IP was returned for each URL.</p>
  
  <p>In output file, output is stored as </p>
  
  <pre><code>URL 
  IP address
  </code></pre>
  
  <p>using uniq -c command I get the count when same IP addresses are adjacent but not when same IP addresses are on non-adjacent line</p>
  
  <pre><code>Command is 
  cat file.log | awk '{print $1}' | uniq -c
  </code></pre>
  
  <p>here is the sample output </p>
  
  <pre><code>1 url
  3 72.51.46.230
  </code></pre>
  
  <p>Now if multiple IP addresses are returned for a particular URL and they are on non-adjacent lines because I have run no. of iterations. In that case uniq-c command will not work. If I use sort option it sorts but I need to display the output as above for each URL ie. URL and next line with the count and its IP address.</p>
  
  <p>For eg. if I do nslookup on google.com it will return multiple addresses and I do uniq -c I get following output. As you see there are same IP addresses but count is only 1 as uniq -c does not work on non-adjacent lines.</p>
  
  <pre><code>  1 74.125.236.64
    1 74.125.236.78
    1 74.125.236.67
    1 74.125.236.72
    1 74.125.236.65
    1 74.125.236.73
    1 74.125.236.70
    1 74.125.236.66
    1 74.125.236.68
    1 74.125.236.71
    1 74.125.236.69
    1 nslookup: can't resolv 'google.com'
    1 nslookup: can't resolv 'google.com'
    1 nslookup: can't resolv 'google.com'
    1 nslookup: can't resolv 'google.com'
    1 nslookup: can't resolv 'google.com'
    1 nslookup: can't resolv 'google.com'
    1 nslookup: can't resolv 'google.com'
    1 74.125.236.70
    1 74.125.236.66
    1 74.125.236.68
    1 74.125.236.71
    1 74.125.236.69
  </code></pre>
  
  <p>I tried with AWK as well but in that case output is not formatted as I require.</p>
  
  <p>Awk command </p>
  
  <pre><code>awk '{a[$0]++}END{for (i in a) printf "%-2d -&gt; %s \n", a[i], i}' file.log
  </code></pre>
  
  <p>Can you suggest a better solution to achieve this -
   Get count and Display in the format as mentioned above? </p>
  
  <p>Output format desired is </p>
  
  <pre><code>URL
  Count IP address
  </code></pre>
  
  <p>sample input file.</p>
  
  <pre><code>URL1
  72.51.46.230
  72.51.46.230
  google.com
  74.125.236.64
  74.125.236.78
  (null)
  nslookup: can't resolv 'google.com'
  nslookup: can't resolv 'google.com'
  nslookup: can't resolv 'google.com'
  nslookup: can't resolv 'google.com'
  nslookup: can't resolv 'google.com'
  </code></pre>
  
  <p>Sample Output required as</p>
  
  <pre><code>URL1
  2 72.51.46.230
  google.com
  1 74.125.236.64
  1 74.125.236.78
  1 null
  5 nslookup: can't resolv 'google.com'
  </code></pre>
  
  <p>Thank you.</p>