title: How to replace string in a file
body: <p>I am writing shell script which will list the drivers .ko files and adding entry in init file. File comes in initramfs.</p>
  
  <p>so below some thing i have did in script, I tried google but solutions not working so finally i had to post question.</p>
  
  <p>few issues,</p>
  
  <p>1) i was trying to store strings with newline character in a variable, but it makes it in a single line, so i used different approach i written a file which has data as expected.</p>
  
  <p>2) Now i want the content with new line chars to be placed in middle of init file, which is below some driver entry so i decided to find last driver, 
  then replace my content with driver and then again append that driver back in file.</p>
  
  <p>3)But i am not able to replace text so i used again different approach to write new file line by line and when i will find driver add my content and append driver back in file. But now content i saved in earlier file appended in a single line no newline character it.</p>
  
  <p>I am not an expert in shell i am doing all it with google. below is my code</p>
  
  <pre><code>    #!/bin/bash
      rm tmp.txt
      handled_drivers=()
      for driver in `cat lib/modules/2.6.18-419.el5/modules.dep | tr -d ":" | tr " " "\n"`
      do
              handled=0
              for d in ${handled_drivers[@]}
              do
                      if [ "$d" == "$driver" ]
                      then
                         handled=1
                      fi
              done
              if [ $handled -eq 0 ]
              then
                   handled_drivers+=($driver)
                   echo "insmod $driver" &gt;&gt; tmp.txt
                   echo "echo \"Loading $(basename "$driver") module\"" &gt;&gt; tmp.txt
              fi
      done
  
      value=`cat tmp.txt`
  
      find_last_driver=`cat initcopy | grep insmod | tail -1| tr -d "\n"`
  
      #
      #if [[ -z $find_last_driver ]];then
      #   echo "Not Found driver skip injection"
      #else
      #   echo "here $find_last_driver"
      #   sed -i -e 's/$find_last_driver/$value/g' ./a.txt
      #fi
  
      while IFS= read -r line;do
          if [ "$line" == "$find_last_driver" ]
          then
          echo $value &gt;&gt;  init_new
          echo $line &gt;&gt; init_new
      else
          echo $line &gt;&gt; init_new
      fi
  done &lt; "init"
  </code></pre>
  
  <p>Below is content of the output file:</p>
  
  <pre><code>#!/bin/nash
  
  mount -t proc /proc /proc
  setquiet
  echo Mounting proc filesystem
  echo Mounting sysfs filesystem
  mount -t sysfs /sys /sys
  echo Creating /dev
  mount -o mode=0755 -t tmpfs /dev /dev
  mkdir /dev/pts
  mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
  mkdir /dev/shm
  mkdir /dev/mapper
  echo Setting up hotplug.
  hotplug
  insmod /lib/uhci-hcd.ko
  mount -t usbfs /proc/bus/usb /proc/bus/usb
  echo "Loading jbd.ko module"
  insmod /lib/jbd.ko
  echo "Loading ext3.ko module"
  insmod /lib/ext3.ko
  echo "Loading scsi_mod.ko module"
  insmod /lib/dm-message.ko
  echo "Loading dm-raid45.ko module"
  insmod /lib/modules/2.6.18-419.el5/kernel/drivers/ata/ata_piix.ko echo "Loading ata_piix.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/ata/libata.ko echo "Loading libata.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/scsi_mod.ko echo "Loading scsi_mod.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/crypto/crypto_algapi.ko echo "Loading crypto_algapi.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/crypto/crypto_api.ko echo "Loading crypto_api.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-crypt.ko echo "Loading dm-crypt.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-mod.ko echo "Loading dm-mod.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-emc.ko echo "Loading dm-emc.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-multipath.ko echo "Loading dm-multipath.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh.ko echo "Loading scsi_dh.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-hp-sw.ko echo "Loading dm-hp-sw.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-log.ko echo "Loading dm-log.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-mem-cache.ko echo "Loading dm-mem-cache.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-message.ko echo "Loading dm-message.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-mirror.ko echo "Loading dm-mirror.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-raid45.ko echo "Loading dm-raid45.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-region_hash.ko echo "Loading dm-region_hash.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-rdac.ko echo "Loading dm-rdac.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-round-robin.ko echo "Loading dm-round-robin.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-snapshot.ko echo "Loading dm-snapshot.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-zero.ko echo "Loading dm-zero.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/usb/net/dm9601.ko echo "Loading dm9601.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/usb/net/usbnet.ko echo "Loading usbnet.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/dma/dmaengine.ko echo "Loading dmaengine.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/hwmon/dme1737.ko echo "Loading dme1737.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/i2c/i2c-core.ko echo "Loading i2c-core.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/net/tulip/dmfe.ko echo "Loading dmfe.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/mpt2sas/mpt2sas.ko echo "Loading mpt2sas.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/scsi_transport_sas.ko echo "Loading scsi_transport_sas.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptbase.ko echo "Loading mptbase.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptctl.ko echo "Loading mptctl.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptfc.ko echo "Loading mptfc.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptscsih.ko echo "Loading mptscsih.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptlan.ko echo "Loading mptlan.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptsas.ko echo "Loading mptsas.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptspi.ko echo "Loading mptspi.ko module" insmod /lib/modules/2.6.18-419.el5/misc/pvscsi.ko echo "Loading pvscsi.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh_alua.ko echo "Loading scsi_dh_alua.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh_emc.ko echo "Loading scsi_dh_emc.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh_rdac.ko echo "Loading scsi_dh_rdac.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/pci/hotplug/shpchp.ko echo "Loading shpchp.ko module" 
  insmod /lib/dm-raid45.ko
  echo Waiting for driver initialization.
  stabilized --hash --interval 1000 /proc/scsi/scsi
  mkblkdevs
  echo Mounting root filesystem.
  mount /sysroot
  echo Setting up other filesystems.
  setuproot
  echo Switching to new root and running init.
  switchroot
  </code></pre>