Skip to main content
added 1 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/^10\^#10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed -i '/^10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/^10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed -i '/^10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/^#10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed -i '/^10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

added 3 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/10\^10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed -i '/10\^10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed '/10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/^10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed -i '/^10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

added 3 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/10\.4/{s/#^#//}' /etc/hosts

To add a comment to those lines:

sed '/10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Jason'sMichael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/10\.4/{s/#//} /etc/hosts

To add a comment to those lines:

sed '/10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Jason's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

To remove comments from lines that have an IP starting with 10.4 (this will remove the first #, I am assuming you will never have more):

sed -i '/10\.4/{s/^#//}' /etc/hosts

To add a comment to those lines:

sed '/10\.4/{s/^/#/}' /etc/hosts

So, why was yours failing? You were using c\ which means:

c<br> text

Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a , which are removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

So, when you ran sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts, this meant "replace #10\.4\..* with the literal string #10\.4\..*". What you wanted to do is:

sed -i 's/#10\.4\.\(.*\)/10.4.\1/' /etc/hosts`

The above uses a substitution operator (s/from/to/) and pattern capturing, the first pattern in \( \) can be referred to as \1, the second as \2 etc. As you can see from Michael's answer this is not needed either, you can just replace #10.4 with 10.4. Or, as in my answer above, you can simply remove the # alone.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading