Real newbie question

Hi all,

I'm hope you all could help me with a very simple question.

I have a multi line text file laid out as below.

10.10.10.45 bobs
10.10.10.34 jims
10.10.10.27 jacks
......
......

I would like to that the 10.10.10.??? and move it to the end of the same
line so the output will look like.

bobs 10.10.10.45
jims 10.10.10.34
jacks 10.10.10.27
......
......

Thanks all,

Vaughan


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Vaughan Williams [ Mi, 21 Juli 2010 20:51 ] [ ID #2044898 ]

Re: Real newbie question

On 7/21/10 Wed Jul 21, 2010 11:51 AM, "Vaughan Williams"
<vaughanw50 [at] gmail.com> scribbled:

> Hi all,
>
> I'm hope you all could help me with a very simple question.
>
> I have a multi line text file laid out as below.
>
> 10.10.10.45 bobs
> 10.10.10.34 jims
> 10.10.10.27 jacks
> .....
> .....
>
> I would like to that the 10.10.10.??? and move it to the end of the same
> line so the output will look like.
>
> bobs 10.10.10.45
> jims 10.10.10.34
> jacks 10.10.10.27
> .....
> .....

perl -ane 'print join(" ",reverse( [at] F)),"\n"' file



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mi, 21 Juli 2010 21:05 ] [ ID #2044900 ]

Re: Real newbie question

>>>>> "VW" == Vaughan Williams <vaughanw50 [at] gmail.com> writes:

VW> Hi all,
VW> I'm hope you all could help me with a very simple question.

VW> I have a multi line text file laid out as below.

VW> 10.10.10.45 bobs
VW> 10.10.10.34 jims
VW> 10.10.10.27 jacks
VW> .....
VW> .....

VW> I would like to that the 10.10.10.??? and move it to the end of the same
VW> line so the output will look like.

VW> bobs 10.10.10.45
VW> jims 10.10.10.34
VW> jacks 10.10.10.27

ok, that is a simple enough problem. what have you tried so far? i could
quickly write a one liner but that wouldn't teach you much. this list is
about learning perl, not getting programs (however simple) written for
you. so i will walk through the ideas needed and you should be able to
code them up.

first off you need to read in the file. this can be done line by line or
as the whole file if it is small enough (and small is megabytes these
days). given this is some form of hosts file (a good guess) it will be
small enough. so write code to read it in and loop over each line.

the next step is to flip the fields. this can be done many different
ways. you first need to get the 2 parts and then recombine them in the
other order. split or a regex will get the parts for you. either is easy
enough to code up even for a newbie but split would be simpler i
think.

then combining those 2 parts back into a line is very easy and can be
done with a simple "" string.

the final step is just printing out the lines. you can print them to
stdout (default for print) and save the output into a file with a shell
redirect (>). or you could open the file in perl (before the loop!) and
print each line to that. again, this is easy perl.

so each step is very easy. do them all and you will have a working perl
program that does what you want. if you get stuck, write back to the
list and show the code and explain your problem.

thanx,

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Mi, 21 Juli 2010 21:06 ] [ ID #2044901 ]

Re: Real newbie question

Hi Uri, Jim,

Thanks for the help, it is a very small file some 8kB in size.

Jim's supplied command worked perfectly, although I really do not
understand it being a 1 day old perl "want-a-be" LOL

I guess have allot of learning to do, can anyone suggest a good starting
point books or online courses for true newbies.

Thanks again for your help,

Vaughan

On Wed, 2010-07-21 at 15:06 -0400, Uri Guttman wrote:
> >>>>> "VW" == Vaughan Williams <vaughanw50 [at] gmail.com> writes:
>
> VW> Hi all,
> VW> I'm hope you all could help me with a very simple question.
>
> VW> I have a multi line text file laid out as below.
>
> VW> 10.10.10.45 bobs
> VW> 10.10.10.34 jims
> VW> 10.10.10.27 jacks
> VW> .....
> VW> .....
>
> VW> I would like to that the 10.10.10.??? and move it to the end of the same
> VW> line so the output will look like.
>
> VW> bobs 10.10.10.45
> VW> jims 10.10.10.34
> VW> jacks 10.10.10.27
>
> ok, that is a simple enough problem. what have you tried so far? i could
> quickly write a one liner but that wouldn't teach you much. this list is
> about learning perl, not getting programs (however simple) written for
> you. so i will walk through the ideas needed and you should be able to
> code them up.
>
> first off you need to read in the file. this can be done line by line or
> as the whole file if it is small enough (and small is megabytes these
> days). given this is some form of hosts file (a good guess) it will be
> small enough. so write code to read it in and loop over each line.
>
> the next step is to flip the fields. this can be done many different
> ways. you first need to get the 2 parts and then recombine them in the
> other order. split or a regex will get the parts for you. either is easy
> enough to code up even for a newbie but split would be simpler i
> think.
>
> then combining those 2 parts back into a line is very easy and can be
> done with a simple "" string.
>
> the final step is just printing out the lines. you can print them to
> stdout (default for print) and save the output into a file with a shell
> redirect (>). or you could open the file in perl (before the loop!) and
> print each line to that. again, this is easy perl.
>
> so each step is very easy. do them all and you will have a working perl
> program that does what you want. if you get stuck, write back to the
> list and show the code and explain your problem.
>
> thanx,
>
> uri
>



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Vaughan Williams [ Mi, 21 Juli 2010 21:16 ] [ ID #2044902 ]

Re: Real newbie question

> I guess have allot of learning to do, can anyone suggest a good starting
> point books or online courses for true newbies.

Learning Perl, Fifth Edition
http://oreilly.com/catalog/9780596520113/
Best beginning Perl book. Perl Cookbook is great for examples as well.

Perl 1: Introduction to Perl
http://www.oreillyschool.com/courses/perl1/
Online Course, Pricy but very good.

Tizag Tutorials
http://www.tizag.com/
Free and pretty good.

Matt

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
matt [ Mi, 21 Juli 2010 22:05 ] [ ID #2044904 ]

Re: Real newbie question

On Wednesday 21 Jul 2010 22:16:38 Vaughan Williams wrote:
> Hi Uri, Jim,
>
> Thanks for the help, it is a very small file some 8kB in size.
>
> Jim's supplied command worked perfectly, although I really do not
> understand it being a 1 day old perl "want-a-be" LOL
>
> I guess have allot of learning to do, can anyone suggest a good starting
> point books or online courses for true newbies.

We've concentrated a lot of resources here:

http://perl-begin.org/

Regards,

Shlomi Fish


>
> Thanks again for your help,
>
> Vaughan
>

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Aphorisms - http://www.shlomifish.org/humour.html

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mi, 21 Juli 2010 22:35 ] [ ID #2044905 ]

Re: Real newbie question

>>>>> "M" == Matt <lm7812 [at] gmail.com> writes:

M> Perl 1: Introduction to Perl
M> http://www.oreillyschool.com/courses/perl1/
M> Online Course, Pricy but very good.

i will second that as i know the author very well. level 2 will be out
pretty soon. there will be 4 levels at least. these are not kiddie
tutorials but real class sessions on line. they offer college credit for
them too.

thanx,

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 22 Juli 2010 00:39 ] [ ID #2044969 ]

Re: Real newbie question

>>>>> "JG" == Jim Gibson <jimsgibson [at] gmail.com> writes:

JG> On 7/21/10 Wed Jul 21, 2010 11:51 AM, "Vaughan Williams"
JG> <vaughanw50 [at] gmail.com> scribbled:

>> Hi all,
>>
>> I'm hope you all could help me with a very simple question.
>>
>> I have a multi line text file laid out as below.
>>
>> 10.10.10.45 bobs
>> 10.10.10.34 jims
>> 10.10.10.27 jacks
>> .....
>> .....
>>
>> I would like to that the 10.10.10.??? and move it to the end of the same
>> line so the output will look like.
>>
>> bobs 10.10.10.45
>> jims 10.10.10.34
>> jacks 10.10.10.27
>> .....
>> .....

JG> perl -ane 'print join(" ",reverse( [at] F)),"\n"' file

adding -l will eliminate the need for the "\n".

since you gave a fish (which i didn't want to do), here is my take
(untested):

perl -pe 's/(\S+)\s+(\S+)/$2 $1/' file

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 22 Juli 2010 00:42 ] [ ID #2044970 ]

Re: Real newbie question

On Jul 21, 12:05=A0pm, jimsgib... [at] gmail.com (Jim Gibson) wrote:
> On 7/21/10 Wed =A0Jul 21, 2010 =A011:51 AM, "Vaughan Williams"
> <vaughan... [at] gmail.com> scribbled:
>
>> ...
>
> > bobs 10.10.10.45
> > jims 10.10.10.34
> > jacks 10.10.10.27
> > .....
> > .....
>
> perl -ane 'print join(" ",reverse( [at] F)),"\n"' file

A variant using the handy -l switch:

perl -lane "print join ' ',reverse [at] F" file

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Mi, 21 Juli 2010 23:39 ] [ ID #2044979 ]

Re: Real newbie question

Jim Gibson wrote:
> <vaughanw50 [at] gmail.com> scribbled:

>> I have a multi line text file laid out as below.
>>
>> 10.10.10.45 bobs
>> 10.10.10.34 jims
>> 10.10.10.27 jacks
>>
>> I would like to that the 10.10.10.??? and move it to the end of the same
>> line so the output will look like.
>>
>> bobs 10.10.10.45
>> jims 10.10.10.34
>> jacks 10.10.10.27
>
> perl -ane 'print join(" ",reverse( [at] F)),"\n"' file

perl -anle 'print " [at] F[1,0]"' file

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
rvtol+usenet [ Do, 22 Juli 2010 08:34 ] [ ID #2044982 ]

Re: Real newbie question

On Jul 21, 3:42=A0pm, u... [at] StemSystems.com ("Uri Guttman") wrote:
> >>>>> "JG" =3D=3D Jim Gibson <jimsgib... [at] gmail.com> writes:
>
> =A0 JG> On 7/21/10 Wed =A0Jul 21, 2010 =A011:51 AM, "Vaughan Williams"
> =A0 JG> <vaughan... [at] gmail.com> scribbled:
>
> =A0 >> Hi all,
> =A0 >>
> =A0 >> I'm hope you all could help me with a very simple question.
> =A0 >>
> =A0 >> I have a multi line text file laid out as below.
> =A0 >>
> =A0 >> 10.10.10.45 bobs
> =A0 >> 10.10.10.34 jims
> =A0 >> 10.10.10.27 jacks
> =A0 >> .....
> =A0 >> .....
> =A0 >>
> =A0 >> I would like to that the 10.10.10.??? and move it to the end of th=
e same
> =A0 >> line so the output will look like.
> =A0 >>
> =A0 >> bobs 10.10.10.45
> =A0 >> jims 10.10.10.34
> =A0 >> jacks 10.10.10.27
> =A0 >> .....
> =A0 >> .....
>
> =A0 JG> perl -ane 'print join(" ",reverse( [at] F)),"\n"' file
>
> adding -l will eliminate the need for the "\n".
>
> since you gave a fish (which i didn't want to do), here is my take
> (untested):
>
> =A0 =A0 =A0 =A0 perl -pe 's/(\S+)\s+(\S+)/$2 $1/' file
>

Since the weather's good for golf...

perl -pale ' $_=3Djoin " ", reverse [at] F' file
perl -ple ' /\s+/;$_=3D"$' $`" ' file

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Do, 22 Juli 2010 10:18 ] [ ID #2044983 ]

Re: Real newbie question

>>>>> "CD" =3D=3D C DeRykus <derykus [at] gmail.com> writes:

CD> On Jul 21, 3:42=A0pm, u... [at] StemSystems.com ("Uri Guttman") wrote:
>>
>> =A0 =A0 =A0 =A0 perl -pe 's/(\S+)\s+(\S+)/$2 $1/' file
>>

CD> Since the weather's good for golf...

i was vaguely hinting at golf but i wanted a fairly normal looking
solution as well!

CD> perl -pale ' $_=3Djoin " ", reverse [at] F' file

that is longer than mine!

CD> perl -ple ' /\s+/;$_=3D"$' $`" ' file

what if there are leading or trailing spaces in the data source! :)

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 22 Juli 2010 18:44 ] [ ID #2044989 ]

Re: Real newbie question

--0015174bddb8703c07048bff39e3
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Jul 22, 2010 at 2:34 AM, Dr.Ruud
<rvtol+usenet [at] isolution.nl<rvtol%2Busenet [at] isolution.nl>
> wrote:

> Jim Gibson wrote:
>
>> <vaughanw50 [at] gmail.com> scribbled:
>>
>
> I have a multi line text file laid out as below.
>>>
>>> 10.10.10.45 bobs
>>> 10.10.10.34 jims
>>> 10.10.10.27 jacks
>>>
>>> I would like to that the 10.10.10.??? and move it to the end of the same
>>> line so the output will look like.
>>>
>>> bobs 10.10.10.45
>>> jims 10.10.10.34
>>> jacks 10.10.10.27
>>>
>>
>> perl -ane 'print join(" ",reverse( [at] F)),"\n"' file
>>
>
> perl -anle 'print " [at] F[1,0]"' file
>
>
perl -plae '$_=" [at] F[1,0]"' file


> --
> Ruud


Scott
--
print+ [at] {[q% [at] %x5**2^q"
534`!./4(%2`0%2,`(!#+%2l"]};

--0015174bddb8703c07048bff39e3--
Scott Hall [ Do, 22 Juli 2010 21:50 ] [ ID #2044993 ]
Perl » gmane.comp.lang.perl.beginners » Real newbie question

Vorheriges Thema: META - Bounces from the list
Nächstes Thema: Word boundaries