Adding to the front of a line
Ray Parrish
crp at cmc.net
Tue Jan 20 03:33:50 UTC 2009
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
--
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