Adding to the front of a line

Ray Parrish crp at cmc.net
Tue Jan 20 05:12:45 UTC 2009


Ray Parrish wrote:
> Matthew Flaschen wrote:
>   
>> Matthew Flaschen wrote:
>>   
>>     
>>> Ray Parrish wrote:
>>>     
>>>       
>>>> Hello,
>>>>
>>>> I've downloaded a text file of the new sites to block to avoid the 
>>>> conficker worm that is currently infecting Windows users, and would like 
>>>> to send it to my Windows using friends and family. However the file 
>>>> unfortunately has only the host names, one per line. I would like to add 
>>>> the 127.0.0.1 and a space to the front of each line in the file before I 
>>>> send it out, so all they have to do is copy and paste it's contents to 
>>>> the end of their hosts files.
>>>>       
>>>>         
>>> There are many ways.  Here's one:
>>>
>>> while read site; do
>>> echo "$site 127.0.0.1"
>>> done < sites.txt > output_file.txt
>>>     
>>>       
>> Whoops.  That was backwards.  It should be:
>>
>> while read site; do
>> echo "127.0.0.1 $site "
>> done < sites.txt > output_file.txt
>>
>>   
>>     
> Hello again,
>
> 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-)
>
> I'm off to the string handling section of my favorite bash scripting 
> site to learn how to strip the line feed off of the end of $site at the 
> start of the loop.
>
> Thanks again for the excellent start, and tipping me off to how easy it 
> is to read in files with a while loop. Here is what I've done so far to 
> make the code more universally useful to me.
>
> #!/usr/bin/env bash
> # Add a text string to the front of each line in a file.
> # Command line parameters $1 - file to read in $2 - string to add to 
> front of line $3 - output filename
> #
> if [ $1 == "" ] || [ $2 == "" ] || [ $3 == "" ]
>    then echo "Usage: AddStringtoLineFront.sh InputfileName StringToAdd 
> OutPutFileName";
> else
>    while read site; do
>    echo "$2 $site "
>    done < $1 > $3
> fi
> exit
>
> Later, Ray Parrish
>
>   
LOL! Well, I should have came back to my email sooner, and read all of 
these excellent replies, it would have saved me a lot of head 
scratching! Thanks for the tips on different methods of accomplishing 
this task. After correcting my if clause to the one that Mathew provided 
I found the following to work as I needed.

#!/usr/bin/env bash
# Add a text string to the front of each line in a file.
# Command line parameters $1 - file to read in $2 - string to add to 
front of line $3 - output filename
#
if [[ $1 == "" ]] || [[ $2 == "" ]] || [[ $3 == "" ]]
   then echo "Usage: AddStringtoLineFront InputfileName StringToAdd 
OutPutFileName";
else
   while read site; do
   echo -n "$2 $site"
   done < $1 > $3
fi
exit

All I had to do in the end, was RTFM for the echo command to finally 
discover the easy way to remove the line feed with -n, and remove the 
extra space character after $site. This was after many tries with the tr 
command, and much research online, in an attempt to find a command which 
would allow me to strip the newline character from the $site variable. 
Most examples I found would strip all newlines out of a file, but didn't 
seem to accept just a variable as input.

I will do some adaptations of this script with the one line methods 
described by the others as well, as it never hurts to know more than one 
method to accomplish something useful.

Thanks again for all the help, I just wish I had noticed it sooner. 8-)

Later, Ray Parrish

-- 
http://www.rayslinks.com/ Web index of human reviewed links.
<http://www.rayslinks.com/Troubleshooting%20and%20fixing%20Windows.html>
Trouble shooting and Fixing Windows
http://www.writingsoftheschizophrenic.com My poetry in web pages





More information about the ubuntu-users mailing list