Skip to main content
made the target string more intrinsically clear
Source Link
Nikole
  • 585
  • 4
  • 8
  • 16

Basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4.10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all.

What am I doing wrong?

Basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all.

What am I doing wrong?

Basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all.

What am I doing wrong?

removed thanks
Source Link
Timo
  • 6.5k
  • 1
  • 28
  • 30

basicallyBasically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all. Clearly I'm not getting something, so any help would be much appreciated. Thanks to all in advance!

What am I doing wrong?

basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all. Clearly I'm not getting something, so any help would be much appreciated. Thanks to all in advance!

Basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all.

What am I doing wrong?

Source Link
Nikole
  • 585
  • 4
  • 8
  • 16

SED change only start of line and preserve end of line

basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the changes I need. Basically I am trying to preserve the whole line for some IP's and comment out other IP's.

For IP's that start with 10.4. To uncomment via sed, I've attempted this

sed -i '/#10\.4\..*/c\10\.4\..*' /etc/hosts

which replaced all the matching lines with 10.4..* instead of preserving the whole line. I've also attempted this in an attempt to use a group/match:

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

but this just silently fails without making any changes at all. Clearly I'm not getting something, so any help would be much appreciated. Thanks to all in advance!