I have a file (ip.txt) that contains ip rules

/sbin/iptables -A INPUT -p tcp -s -j DROP
/sbin/iptables -A INPUT -p tcp -s -j DROP

/sbin/iptables -A INPUT -p udp -s -j DROP
/sbin/iptables -A INPUT -p tcp -s -j DROP

I am using below bash script (removeip.sh) to remove udp and tcp lines that belong to a specific IP from the ip.txt file .

function removeip(){
    udprule="/sbin/iptables -A INPUT -p udp -s "$ip" -j DROP"
    tcprule="/sbin/iptables -A INPUT -p tcp -s "$ip" -j DROP"
    sed --in-place /"$udprule"/d /root/ip.txt
    sed --in-place /"$udprule"/d /root/ip.txt     

removeip $1

When I use the below command in terminal


it throws

sed: -e expression #1, char 59: unterminated `s' command

What is the cause of this issue and how can I fix it?


The / character is an operator in sed. Basically you are ending up with:

sed '//sbin/iptables -A INPUT -p tcp -s "{ip}" -j DROP/d' /root/ip.txt

This means that you are trying to execute '//sb' as a sed command, where b is the separator, and it cannot find another 'b'.

You would need to quote the special characters:

udprule="\/sbin\/iptables -A INPUT -p udp -s "$ip" -j DROP"

And you would need to watch out for what is in $ip for the same reason.

  • Alternatively, sed allows you to use any character instead of /. Pick one that wouldn't come up in the text. For example, s_pattern_replacement_g should work – user68488 Oct 5 '14 at 20:38

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.