I am in need of a solution, preferably sed, that can match on an IP octet that contains any number combination and remove the line. For example:

My file:

I need to remove the line that contains any IP found on the 10.10.20.x subnet and remove it. So, if the file contains , 20.11, 20.12, etc... They all get removed. I also need an inline replacement.

What I have tried:

sed -i '/10\.10\.20\.\([0-9]\{1,3\}\)/d' file

and this:

sed -i '/10.\.10\.20\.*\([0-9]\{1,3\}\)*/d' file​

And neither one works. The command completes with no errors, but the lines are still in the file. I am also open to other solutions such as awk.

1 Answer 1


With GNU sed:

sed -i '/10\.10\.20\.[[:digit:]]\{,3\}/d' file

With BSD sed, you need sed -i ''.

  • Perfect thanks! But why does [0-9] not work? I have seen it in other examples here and there?
    – user53029
    May 9, 2016 at 2:04
  • 1
    @user53029: sed -i '/10\.10\.20\.\([0-9]\{1,3\}\)/d' file should work, but [0-9] can match other than digit 1 to 9, base on your system and locale. [:digit:] guarantees to match anything considered digits in current locale.
    – cuonglm
    May 9, 2016 at 2:25

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.