sorry for the typo, the last command should be the below: ~$ sed -e '/127.0.0.1/d' -e "1i 127.0.0.1 localhost\n127.0.0.1 $HOSTNAME.domainname $HOSTNAME" testfile 127.0.0.1 localhost 127.0.0.1 node1.domainname node1 # comment ::1 localhost ip6-localhost ip6-loopback 2017-06-14 11:48 GMT+08:00 Han Lu : > Hi Team, > > Please check this below: > > ~$ cat /etc/os-release > NAME="Ubuntu" > VERSION="14.04.5 LTS, Trusty Tahr" > ID=ubuntu > ID_LIKE=debian > PRETTY_NAME="Ubuntu 14.04.5 LTS" > VERSION_ID="14.04" > HOME_URL="http://www.ubuntu.com/" > SUPPORT_URL="http://help.ubuntu.com/" > BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" > > ~$ sed --version > sed (GNU sed) 4.2.2 > Copyright (C) 2012 Free Software Foundation, Inc. > License GPLv3+: GNU GPL version 3 or later html>. > This is free software: you are free to change and redistribute it. > There is NO WARRANTY, to the extent permitted by law. > > Written by Jay Fenlason, Tom Lord, Ken Pizzini, > and Paolo Bonzini. > GNU sed home page: . > General help using GNU software: . > E-mail bug reports to: . > Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. > > > Problem reproduce process: > > ~$ cat testfile > 127.0.0.1 localhost hostname > ::1 localhost ip6-localhost ip6-loopback > > The below command works as expected. > ~$ sed -e 's/127.0.0.1/127/' -e "1i 127.0.0.1 localhost\n127.0.0.1 > $HOSTNAME.domainname $HOSTNAME" testfile > 127.0.0.1 localhost > 127.0.0.1 node1.domainname node1 > 127 localhost hostname > ::1 localhost ip6-localhost ip6-loopback > > But command below is not working as expeced: > ~$ sed -e '/127.0.0.1/d' -e "1i 127.0.0.1 localhost\n127.0.0.1 > $HOSTNAME.domainname $HOSTNAME" testfile > ::1 localhost ip6-localhost ip6-loopback > > expected output: > 127.0.0.1 localhost > 127.0.0.1 node1.domainname node1 > ::1 localhost ip6-localhost ip6-loopback > > If I add a comment at first line. then it's working. > ~$ cat testfile > # comment > 127.0.0.1 localhost hostname > ::1 localhost ip6-localhost ip6-loopback > > ~$ sed -e '/127.0.0.1/d' -e "1i 127.0.0.2 localhost\n127.0.0.2 > $HOSTNAME.domainname $HOSTNAME" testfile > 127.0.0.2 localhost > 127.0.0.2 node1.domainname node1 > # comment > ::1 localhost ip6-localhost ip6-loopback > >