I try to use sed command inside a loop to replace IP address, but it is always giving me this error:

sed: -e expression #1, char 15: unterminateds' command`

even it is not missing the end slash.

Example format of the IP address:

Here is my code:

readarray thearray < /root/scripts/ipaddr.info

 for item in ${!thearray[@]}; do
    echo -e "IP: ${thearray[$item]}."
    echo -e "Change this IP? (y/n)"
    read Useranswer
    if [ $Useranswer = y ]; then
        echo -e "Please type the IP address:"
        read Firstipaddress



        # do the change on all files
        sed -i "s/$oldipaddr/$Firstipaddress/g" /root/scripts/ipaddr.info
        sed -i "s/$oldipaddr/$Firstipaddress/g" /etc/application/config
        sed -i "s/$oldipaddr/$Firstipaddress/g" $Filepath
        sed -i "s/$new_oldipaddr/$new_ipaddr/g" $Filepath

    elif [ $Useranswer = n ]; then

What will be the problem here and how to solve it?

  • 1
    I suggest that you print the values of $oldipaddr, $Firstipaddress, $new_oldipaddr, and $new_ipaddr before the calls to sed to see what you're passing to the application. Do those variables have any unexpected symbols? – Andy Dalton Dec 21 '15 at 18:18
  • No unexpected symbols on the value... – Ari Dec 21 '15 at 18:37

Your $oldipaddr (obtained from ${thearray[$item]}) values contain newlines read from /root/scripts/ipaddr.info, which are causing the sed commands to get split across lines, like

Please type the IP address:
sed -i s/
/ /root/scripts/ipaddr.info


readarray -t thearray < ipaddr.info

From help mapfile

      -t        Remove a trailing newline from each line read.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.