FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 01-19-2009, 11:08 PM
Ray Parrish
 
Default Adding to the front of a line

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.

I know that this task is very easy with the Linux command line, but am
not experienced enough yet with bash scripting and command line work to
know how to do it. Could someone please steer me to the commands I need
to read up on in order to accomplish this task?

Thanks, 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@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-19-2009, 11:17 PM
Matthew Flaschen
 
Default Adding to the front of a line

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

Matt Flaschen

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-19-2009, 11:19 PM
Matthew Flaschen
 
Default Adding to the front of a line

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 01:54 AM
Ray Parrish
 
Default 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
>
>
Thank you! My windows using friends and family will thank you too, if
they have actually taken my previous advice and started using a hosts
file. 8-) The article I read about that particular virus claims it has
infected 3.5 million computers so far, pretty sad state of affairs.

Long live Linux, and it's comparatively more secure system.

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@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 02:33 AM
Ray Parrish
 
Default 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@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 02:48 AM
Lorenzo Luengo
 
Default Adding to the front of a line

Matthew Flaschen escribió:
> 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
>
> Matt Flaschen
>
>

I think the easiest is this

sed 's/^/127.0.0.1 /' myfile > mynewfile

--
Lorenzo Luengo Contreras
Administrador de Sistemas DGEO
Universidad de Concepción
Concepción - Chile
+56-41-2207277


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 02:57 AM
Smoot Carl-Mitchell
 
Default Adding to the front of a line

Here is a perl one liner which does what you need:

perl -pi -e 's/^/127.0.0.1 /' file file ...

See "man perlrun" for an explanation of the perl options that make this
work.
--
Smoot Carl-Mitchell
Computer Systems and
Network Consultant
smoot@tic.com
+1 480 922 7313
cell: +1 602 421 9005

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 03:16 AM
Matthew Flaschen
 
Default Adding to the front of a line

Ray Parrish wrote:
> 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-)

Okay, I figured this out. You must have a CRLF file, and I left a space
after $site by mistake which screws things up for that. Use

while read site; do
echo "127.0.0.1 $site"
done < sites.txt > output_file.txt

Or, try this alternative method:

sed "s/.*/127.0.0.1 &/" sites.txt>output_file.txt

Note this also screws up on CRLF if there's a space after the &. It's
equivalent to the above, just terser.

> #!/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 == "" ]

I think you mean:

if [[ $1 == "" ]] || [[ $2 == "" ]] || [[ $3 == "" ]]

The first will work as expected... unless of course any of $1, $2, or $3
is blank in which case you'll get:

bash: [: ==: unary operator expected

or similar (in other shells).

The second will work either way.

Matt Flaschen

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 03:24 AM
Cameron Hutchison
 
Default Adding to the front of a line

Ray Parrish <crp@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.

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-20-2009, 03:31 AM
Matthew Flaschen
 
Default Adding to the front of a line

Cameron Hutchison wrote:
> 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.

I doubt you'll get any noticeable performance difference. The bash
script I gave does not fork any external processes.

Matt Flaschen

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

Thread Tools




All times are GMT. The time now is 12:58 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org