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,642
  • 2
  • 26
  • 43
Tampa
  • 75,446
  • 119
  • 278
  • 425

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
  • 6,198
  • 3
  • 30
  • 42
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,641
  • 4
  • 30
  • 52
Jente
  • 612
  • 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,387
  • 6
  • 35
  • 45