Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
    
See this: thegeekstuff.com/2009/04/… –  nitishch Jun 23 at 9: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: unix.stackexchange.com/questions/112023/… –  jofel Jun 23 at 10:11
    
The downvote button is tagged as, "This question does not show any research effort; it is unclear or not useful". –  roaima Jun 23 at 14:23

3 Answers 3

up vote 0 down vote accepted

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
share|improve this answer
    
This would replace 0a0b0c0 etc. –  User112638726 Jun 23 at 10:10
1  
You need to escape the dots: sed 's/0\.0\.0\.0/127.0.0.1/' .... –  jofel Jun 23 at 10:12
    
Problem solved, what a stupid question!! 's/0\.0\.0\.0/127.0.0.1/' . Backslashes before dots are must. –  Arnab Jun 24 at 3:59

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
share|improve this answer
    
...and in vi, %s/^0\.0\.0\.0/127.0.0.1/ Enter followed by ZZ and Enter could also work. –  roaima Jun 23 at 14:26
perl -lpe 's/^(0\.){3}0/127.0.0.1/' file > newfile

should do the trick.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.