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



I want to generate HOSTS file like

... abc.com

In text file, I have thousand of domain entry.

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

5 Answers 5


If you want the original to be intact:

sed 's/^/ /' domain.txt > new_domain.txt

GUI answer using GEdit

  1. Open up your file in Gedit. Let's say I want to add ' ' 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 at the beginning of the first line.

enter image description here

  • Also good option... Jun 13, 2016 at 5:20

You can achieve that with sed in the following way:

sed -i 's/^/ /' /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/^/ /' /path/filename.txt

A bit more information about sed you can find here.

  • Same thing, how to put after the line? Jun 10, 2016 at 9:20
  • 1
    Use $ instead of ^, to add to the end of line.
    – Videonauth
    Jun 10, 2016 at 9:23

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 " " $1;}' domain.txt

# produces: 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 " " $0;}' domain.txt

# produce: domain.zzz.yy user01 user02
# $0: whole line
  • 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 " " $0}' is a more general answer. Jun 10, 2016 at 18:01
  • Important note @G-Man, I'm going to update the answer.
    – gwarah
    Jun 10, 2016 at 19:08

Easy way to add before :

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

%s means string of line on file

\n means prints the content in new line

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.