0

Based on: sed: Replace part of a line

I want to modify my sysctl.conf file. The line which contains PermitTunnel something must be changed to PermitTunnel point-to-point.

So using as one man said in the post before, I would use:

sed -e 's/PermitTunnel.*$/PermitTunnel point-to-point/g'

Including at the end of the line my file.

Since -n is not used, I guess I'm expected to receive at standar output the result of my operation. Then I executed it and get (notice I'm using -2- instead of -to- just in order to see if I can modify the file as I want, because the file already have the desired line at this case):

root@debian:/home/dit# sed -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf 
PermitTunnel point-2-point

But then I do:

root@debian:/home/dit# cat /etc/sysctl.conf | grep PermitTunnel
PermitTunnel point-to-point

So as you can see, the file has not changed. What am I doing wrong?

Thanks for reading

Community
  • 1
  • 1
Btc Sources
  • 1,912
  • 2
  • 30
  • 58

1 Answers1

6

You command take sysctl.conf as input, and stdout as output. You have to use the -i option to replace "in place"

sed -i -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf 

You can also specify a suffix for a backup file:

sed -i.bak -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf 

From man sed:

-i[SUFFIX], --in-place[=SUFFIX]
     edit files in place (makes backup if extension supplied)

Alternatively, you can redirect stdout to a new file:

sed -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf > /etc/sysctl.conf.new
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • 1
    For information, in some versions of sed (BSD, in particular), the argument of the `-i` flag is mandatory, not optional. – jub0bs Jan 05 '15 at 14:50