7

I have list of domain in text file and I need to add 127.0.0.1 ip before the line in whole file. Example as below:

domain.txt

....
abc.com
...

I want to generate HOSTS file like

...
127.0.0.1 abc.com
...

In text file, I have thousand of domain entry.

  • 1
    Is it alway 127.0.0.1 you want to put there? – Videonauth Jun 10 '16 at 8:51
  • @Videonauth yes – Nullpointer Jun 10 '16 at 8:52
  • You want to put 127.0.0.1 only before the domain abc.com or on all the domains i.e. on all lines? – heemayl Jun 10 '16 at 9:37
6

You can achieve that with sed in the following way:

sed -i 's/^/127.0.0.1 /' /path/filename.txt

Best make a backup of the file before, I'm right now not sure if you need to escape the space or not. To make a backup directly when you run sed you can use the following line:

sed -i_bak -e 's/^/127.0.0.1 /' /path/filename.txt

A bit more information about sed you can find here.

|improve this answer|||||
  • Same thing, how to put after the line? – Nullpointer Jun 10 '16 at 9:20
  • 1
    Use $ instead of ^, to add to the end of line. – Videonauth Jun 10 '16 at 9:23
7

If you want the original to be intact:

sed 's/^/127.0.0.1 /' domain.txt > new_domain.txt
|improve this answer|||||
3

GUI answer using GEdit

  1. Open up your file in Gedit. Let's say I want to add '127.0.0.1 ' to the beginning of all lines in this text file.

enter image description here

  1. Using search and replace feature, search for \n and replace it with \n127.0.0.1 then click Replace all button as shown below:

enter image description here

  1. This is your result:
    Disclaimer: You will have to manually enter the replace with text 127.0.0.1 at the beginning of the first line.

enter image description here

|improve this answer|||||
  • Also good option... – Nullpointer Jun 13 '16 at 5:20
3

awk is a good option to this issue. Explaining in two examples:

# Let's consider a file domain.txt with lines like this:
# <domain> <user> <user> ....
# Ex: domain.zzz.yy user01 user02

# Extracting the first field of each line
awk '{ print "127.0.0.1 " $1;}' domain.txt

# produces: 127.0.0.1 domain.zzz.yy
# filter only first field $1=domain.zzz.yy
# $1: first field, $2: second field, and so on

# Extracting the whole line
awk '{ print "127.0.0.1 " $0;}' domain.txt

# produce: 127.0.0.1 domain.zzz.yy user01 user02
# $0: whole line
|improve this answer|||||
  • Well, this depends on the assumption that each line contains only one "word" (i.e., it contains no spaces), which may be true if each line is a domain name (with no comments).  Otherwise, your answer retains only the first "word" on each line and discards the remainder.  awk '{ print "127.0.0.1 " $0}' is a more general answer. – G-Man Says 'Reinstate Monica' Jun 10 '16 at 18:01
  • Important note @G-Man, I'm going to update the answer. – gwarah Jun 10 '16 at 19:08
0

Easy way to add before :

$ printf "127.0.0.1 %s\n" $(cat domain.txt)

%s means string of line on file

\n means prints the content in new line

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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