title: Bash scripting, two files with the same name in another directories
body: <p>So, I have two files with the same name in two different directories, one is in /usr/local/nagios/etc/hosts/ and the other one in /usr/local/nagios/etc/services/.</p>
  
  <p>I was trying to copy lines in the first file that contains contact* to the other file with the same name in another directory. </p>
  
  <p>First file looks like this:</p>
  
  <pre><code> define host {
      host_name                       10.80.12.62
      address                         10.80.12.79
      check_command                   check-host-alive!!!!!!!!
      max_check_attempts              4
      check_interval                  5
      retry_interval                  2
      check_period                    24x7
      contacts                        Ivan Ivić,Ivana Ivić
      contact_groups                  Testna
      register                        1
      }
  </code></pre>
  
  <p>And another like this:</p>
  
  <pre><code> define service {      
      host_name                       10.80.12.62
      service_description             Current Load
      use                             xiwizard_passive_service
      is_volatile                     1
      max_check_attempts              1
      check_interval                  1
      retry_interval                  1
      check_period                    xi_timeperiod_24x7
      notification_interval           60
      notification_period             xi_timeperiod_24x7
      contacts                        nagiosadmin
      stalking_options                o,w,u,c
      _xiwizard                       passivecheck
      register                        1
      }
  
  define service {
      host_name                       10.80.12.53,10.80.12.62
      service_description             PING
      use                             xiwizard_passive_service
      check_command                   check_ping!100.0,20%!500.0,80%!!!!!!
      is_volatile                     1
      max_check_attempts              1
      check_interval                  1
      retry_interval                  1
      check_period                    xi_timeperiod_24x7
      notification_interval           60
      notification_period             xi_timeperiod_24x7
      contacts                        nagiosadmin
      stalking_options                o,w,c,u,
      _xiwizard                       passivecheck
      register                        1
      }
  </code></pre>
  
  <p>I would like to copy the lines containing contact* from the first file to another file updating or adding the contact* lines in another file.</p>
  
  <p>I was trying something like:</p>
  
  <p>grep contac* /usr/local/nagios/etc/hosts/$file_name >/tmp/contacts</p>
  
  <p>sed -i '/define service {/r/tmp/contacts' /usr/local/nagios/etc/services/$file_name}</p>
  
  <p>But with that, I´m just copying the lines from the first file to another.
  I need to get this result:</p>
  
  <pre><code> define service {
      contacts                        nagiosadmin,Ivan Ivić,Ivana Ivić
      contact_groups                  Testna
      host_name                       10.80.12.62
      service_description             Current Load
      use                             xiwizard_passive_service
      is_volatile                     1
      max_check_attempts              1
      check_interval                  1
      retry_interval                  1
      check_period                    xi_timeperiod_24x7
      notification_interval           60
      notification_period             xi_timeperiod_24x7
      stalking_options                o,w,u,c
      _xiwizard                       passivecheck
      register                        1
      }
  
   define service {
      contacts                        nagiosadmin,Ivan Ivić,Ivana Ivić
      contact_groups                  Testna
      host_name                       10.80.12.53,10.80.12.62
      service_description             PING
      use                             xiwizard_passive_service
      check_command                   check_ping!100.0,20%!500.0,80%!!!!!!
      is_volatile                     1
      max_check_attempts              1
      check_interval                  1
      retry_interval                  1
      check_period                    xi_timeperiod_24x7
      notification_interval           60
      notification_period             xi_timeperiod_24x7
      stalking_options                o,w,c,u,
      _xiwizard                       passivecheck
      register                        1
      }
  </code></pre>