Adding to the front of a line
crp at cmc.net
Tue Jan 20 03:33:50 UTC 2009
Matthew Flaschen wrote:
> Matthew Flaschen wrote:
>> Ray Parrish wrote:
>>> 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
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.
# 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
while read site; do
echo "$2 $site "
done < $1 > $3
Later, Ray Parrish
http://www.rayslinks.com/ Web index of human reviewed links.
Trouble shooting and Fixing Windows
http://www.writingsoftheschizophrenic.com My poetry in web pages
More information about the ubuntu-users