Re: Adding to the front of a line



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


--
ubuntu-users mailing list
ubuntu-users@xxxxxxxxxxxxxxxx
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users



Relevant Pages

  • Re: set user=%username%
    ... Your most recent feedback confirms that Windows is ... Type this: echo %MyName% ... > usertest=This is a long string. ...
    (microsoft.public.windows.server.general)
  • Re: Adding to the front of a line
    ... to send it to my Windows using friends and family. ... echo "127.0.0.1 $site " ... I'm off to the string handling section of my favorite bash scripting ... Later, Ray Parrish ...
    (Ubuntu)
  • Re: why use the sealed ?
    ... system that supports inheritance and polymorphism is to promote reusability. ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... >> exceeds past the end of the string. ... >> application to be a Remoting Client. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XP and string paths
    ... > the "Lcase" function is suddenly no longer available to Excel? ... > that occurs because of Windows XP. ... > string functions that Excel uses has become ... > is the usual "missing reference" problem. ...
    (microsoft.public.excel.programming)
  • Re: Internet connection on Linux
    ... If you are having to use windows to access Usenet: ... If you do not want some malware writing to linux. ... If using linux for Usenet access, su - root, copy script text into xx ... echo "You need to be root to run $0" ...
    (comp.os.linux.networking)