-4

I have a Ad-blocking hosts file with 0.0.0.0 IPs pointing to URLs. How to replace 0.0.0.0 with 127.0.0.1 command line without any visual text editor ? Manually doing this with nano , vi etc. is impossible , the list have above 15k lines.

from this

0.0.0.0  c.one97adworks.com
0.0.0.0  0koryu0.easter.ne.jp
0.0.0.0  static.super-links.net

to this

127.0.0.1  c.one97adworks.com
127.0.0.1  0koryu0.easter.ne.jp
127.0.0.1  static.super-links.net

at once.

My shell bash 4.3.33.

UPDATE two down votes ? please leave comments.

Arnab
  • 1,581
  • See this: http://www.thegeekstuff.com/2009/04/vi-vim-editor-search-and-replace-examples/ – nitishch Jun 23 '15 at 09:56
  • 4
    The question was probably downvoted, as it could have been solved by a minimal search effort (e.g. "text replacement linux"). Here a general answers to your question: http://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files – jofel Jun 23 '15 at 10:11
  • The downvote button is tagged as, "This question does not show any research effort; it is unclear or not useful". – Chris Davies Jun 23 '15 at 14:23

3 Answers3

1
perl -lpe 's/^(0\.){3}0/127.0.0.1/' file > newfile

should do the trick.

Sven
  • 11
1

Note that since the file is structured in this way you can also do this in Vi, nano or any other text editor; simply use its find/replace command.

E.g. in nano, the simplest text editor around:

  1. open the file
  2. CTRLW then CTRLR
  3. enter 0.0.0.0 as search string
  4. enter 127.0.0.1 as replacement string
  5. A to replace all matches
  6. save the edited file
dr_
  • 29,602
0

With the contents in file1.txt, the results will be in file2.txt with the following:

sed 's/^0\.0\.0\.0/127.0.0.1/' file1.txt > file2.txt
sbj3
  • 116