title: Replace IP Address from file
body: <p>Sample.txt file:</p>
  
  <pre><code>.........................
  some log file entries
  some log file entries
  some log file entries
  some log file entries
  This system ip is not found
  some log file entries
  some log file entries
  some log file entries
  This system IP is 172.16.80.10
  some log file entries
  some log file entries
  This system IP:172.16.80.10
  some log file entries
  some log file entries
  some log file entries
  Hostname::ip-172.16.80.10.ec2.internal
  some log file entries
  some log file entries
  ......................
  </code></pre>
  
  <p>I want to replace the IP Address from file not the host name, however it is changing hostname also.</p>
  
  <p>I am using this command to replace IP address:</p>
  
  <pre><code>sed s/172.16.80.10/172.16.80.12/g  sample.txt
  </code></pre>
  
  <p>Getting Output </p>
  
  <pre><code>.........................
  some log file entries
  some log file entries
  some log file entries
  some log file entries
  This system ip is not found
  some log file entries
  some log file entries
  some log file entries
  This system IP is 172.16.80.12
  some log file entries
  some log file entries
  This system IP:172.16.80.12
  some log file entries
  some log file entries
  some log file entries
  Hostname::ip-172.16.80.12.ec2.internal
  some log file entries
  some log file entries
  ......................
  </code></pre>
  
  <p>(Changing Hostname also)</p>
  
  <p>Desired output is </p>
  
  <pre><code>.........................
  some log file entries
  some log file entries
  some log file entries
  some log file entries
  This system ip is not found
  some log file entries
  some log file entries
  some log file entries
  This system IP is 172.16.80.12
  some log file entries
  some log file entries
  This system IP:172.16.80.12
  some log file entries
  some log file entries
  some log file entries
  Hostname::ip-172.16.80.10.ec2.internal
  some log file entries
  some log file entries
  ......................
  </code></pre>
  
  <p>Code example:</p>
  
  <pre><code>while getopts i:h: opt
    do
    case $opt in
    i)
      CurrentLocalIpv4=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`
      if [[ $OPTARG =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        sed -i s/"$CurrentLocalIpv4"/"$OPTARG"/g /home/ec2-user/sample.txt
        echo "ip cahnged"
      else
        echo "fail"
      fi
      ;;
      h)
        echo"hostname"  ;;
     esac
   done
  </code></pre>