Ubuntu – How to add the text before the line in whole file

command linetext processing

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.

Best Answer

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.