title: Shell Script Printing in table format
body: <p>I am trying to print set of values in table format using shell script . The table has n rows and 4 columns. I tried the following piece of code.</p>
  
  <pre><code>btfield=`grep -i -r ^"diag.* &lt;string&gt;" *.txt |awk '{print $5}'|cut -d+ -f 1 |sort -u`
  ipaddr=''
  i=0
  format="%10s\t%10s\t%10s   \n"
  echo "| Function  " "     |         IP         |"  "    occurences     |"  
  for a in $btfield
  do
    b=`grep -i -r ^"diag.* &lt;string&gt;" *.txt |grep  -i $a |cut -d: -f 1|cut -d_ -f 2|cut -d't' -f 1`
    noOcc=`grep -i -r ^"diag.* backtrace" *.txt |grep  -i $a|wc -l`
    #echo $b
    ipaddr[i]=${b}
    printf "$format"  $a  ${ipaddr[i]} $noOcc
    i=$((i+1))
    #echo $i
  done
  </code></pre>
  
  <p>The above piece of code finds distinct fields from various files and prints according to the format specifier.</p>
  
  <p>But what I am seeing is misaligned form of output. Is there any way to print the values in table format ? The column width is fixed and if the value in cell exceeds the width it must wrap itself. </p>
  
  <pre><code>Sample output:
  
  |       reason          |        STB IP         |        occurences     |
  printf     142.25.1.100.   142.25.1.100.   
  142.25.1.102.   142.25.1.105.   192.168.1.100.   
  192.168.1.100.  192.168.1.100.  192.168.1.100.   
  192.168.1.106.           9                   
  class_consol      142.25.1.105.   192.168.1.103.   
           2                                   
  getChar   182.168.1.102.           1   
       maindat      142.25.1.103.            1   
  _XN52getappdatafrom3EZN2232_iPjj     142.25.1.103.   142.25.1.103.   
  182.168.1.106.    
  </code></pre>