0

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:

 192.168.100.12
 192.168.200.12
 10.10.20.10
 10.10.30.20

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 10.10.20.10 , 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

2

With GNU sed:

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

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

2
  • 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.