0

I want to comment out this line in /etc/hosts:

127.0.0.1 test test 

to become:

#127.0.0.1 test test 

How to a one liner in bash command line to do this to find the line that just starts with 127? I am using Ubuntu 12.04.

Thanks

Radu Rădeanu
  • 2,474
  • 2
  • 23
  • 42
Tampa
  • 69,424
  • 111
  • 266
  • 409

3 Answers3

3

You can use sed(1):

sed -i '/^127/s/^/#/' /etc/hosts

-i means to do the substitution in place, so the substitution happens in /etc/hosts, not on stdout which is standard.

in '/^127/s/^/#/', '/^127/' means find a line starting with 127 (^ is the start of line anchor), the s/^/#/ substitute the start of that line with a #.

jbr
  • 5,968
  • 3
  • 29
  • 41
2

I think you can do this with the following command:

sed -i 's/127.0.0.1 test test/#127.0.0.1 test test/g' /etc/hosts
Mark van Lent
  • 12,161
  • 4
  • 28
  • 52
Jente
  • 602
  • 1
  • 8
  • 17
2

Try using sed

sed -i.bak 's/^127/#&/' /etc/hosts

-i.bak - in place replace and create backup file with .bak

jkshah
  • 11,005
  • 6
  • 34
  • 44