Adding to the front of a line

Cameron Hutchison lists at xdna.net
Tue Jan 20 04:24:23 UTC 2009


Ray Parrish <crp at cmc.net> writes:

>Matthew Flaschen wrote:
>>> Ray Parrish wrote:
>>>>
>>>> [...] I would like to add the 127.0.0.1 and a space to the front of 
>>>> each line in the file [...]
>>>>       
>>
>> while read site; do
>> echo "127.0.0.1 $site "
>> done < sites.txt > output_file.txt
>>

>That *almost* worked perfectly, with the exception that it is adding a 
>blank line between each line in the output file which is undesirable. 8-)

Here's another way - a 1 liner:

$ sed -i 's/^/127.0.0.1 /' file.txt

This modifies the file you pass in. If you dont want to do that, remove
-i and redirect output to a new file.

If the file is not very big, looping in bash will work ok, but once you
start dealing with larger files, you really want to use other tools than
bash loops. 




More information about the ubuntu-users mailing list