List Info

Thread: Need second look at this...




Need second look at this...
user name
2007-10-11 13:44:14
trying to grep out some info from my /etc/hosts file...just to get more practice with Perl and well for my personal pleasure, too.
 

#!/usr/bin/perl

open (FILE, "/etc/hosts";);
lines = <FILE&gt;;
close (FILE);

foreach $line (lines){
&nbsp;  if ($line =~ /pm1/){
&nbsp; &nbsp;   print $line;
&nbsp;  }
}

the problem with this is that it's printing out some hosts that i don't want that also have pm1 at the end.

somehostpm1 but it also gets my dr hosts (dr-somehostpm1)

thoughts?

by the way, i had to jump the wifes car so i ended up missing out on the meeting. gonna try to make the next one for sure, though.

Re: Need second look at this...
user name
2007-10-11 15:53:34
my "dr&quot; hosts are my disaster/recovery hosts that i don't use on a daily basis...the other ones are production hosts that i'm using on a daily basis.

On 10/11/07, Garrett Goebel < ggoebelgoebel.ws">ggoebelgoebel.ws> wrote:
In order to fix your regex you'll need to tell us the difference
between somehostpm1 and dr-somehostpm1.

Will a "good" somehostpm1 never be preceeded by 'dr-&#39;? If so...

# Shot at adhering to Perl Best Practices
open my($file), '<&#39;, '/etc/hosts';
while (my $line = <$file&gt;) {
   ; if ($line =~ /(?<!dr-)S+pm1/) {
 &nbsp; &nbsp;   ; print $line;
&nbsp; &nbsp; }
}

# Quick'n Dirty
open FILE, '</etc/hosts&#39;;
map { print if /(?<!dr-)S+pm1/ } <FILE&gt;;

I haven't tested it... perhaps it'll work.

(?<!pattern) is a zero width negative look behind assertion. I.e.
it9;ll match anything that isn't preceeded by the pattern without
including it in the match.

Garrett

On Oct 11, 2007, at 2:44 PM, Emmanuel Mejias wrote:

> trying to grep out some info from my /etc/hosts file...just to get
> more practice with Perl and well for my personal pleasure, too.
>
> #!/usr/bin/perl
>
> open (FILE, "/etc/hosts";);
> lines = <FILE&gt;;
> close (FILE);
&gt;
> foreach $line (lines){
&gt; &nbsp; &nbsp;if ($line =~ /pm1/){
&gt; &nbsp; &nbsp; &nbsp; print $line;
>; &nbsp; &nbsp;}
> }
>
>; the problem with this is that it's printing out some hosts that i
> don't want that also have pm1 at the end.
>
> somehostpm1 but it also gets my dr hosts (dr-somehostpm1)
>
> thoughts?
>
> by the way, i had to jump the wifes car so i ended up missing out
> on the meeting. gonna try to make the next one for sure, though.
&gt;
> _______________________________________________
> kc mailing list
> kcpm.org">kcpm.org
&gt; http://mail.pm.org/mailman/listinfo/kc


Re: Need second look at this...
user name
2007-10-11 15:54:58
On 10/11/07, Emmanuel Mejias <emmanuel.mejiasgmail.com> wrote:
> trying to grep out some info from my /etc/hosts
file...just to get more
> practice with Perl and well for my personal pleasure,
too.
>
> #!/usr/bin/perl
>
> open (FILE, "/etc/hosts");
> lines = <FILE>;
> close (FILE);
>
> foreach $line (lines){
>    if ($line =~ /pm1/){
>       print $line;
>    }
> }
>
>
> the problem with this is that it's printing out some
hosts that i don't want
> that also have pm1 at the end.
>
> somehostpm1 but it also gets my dr hosts
(dr-somehostpm1)
>
> thoughts?

Are the ones you don't want all have a prefix of 'dr-'? Can
we get a
sample list?

> by the way, i had to jump the wifes car so i ended up
missing out on the
> meeting. gonna try to make the next one for sure,
though.
_______________________________________________
kc mailing list
kcpm.org
http://mail.pm
.org/mailman/listinfo/kc

Re: Need second look at this...
country flaguser name
United States
2007-10-11 15:40:17
In order to fix your regex you'll need to tell us the
difference  
between somehostpm1 and dr-somehostpm1.

Will a "good" somehostpm1 never be preceeded by
'dr-'? If so...

# Shot at adhering to Perl Best Practices
open my($file), '<', '/etc/hosts';
while (my $line = <$file>) {
     if ($line =~ /(?<!dr-)S+pm1/) {
         print $line;
     }
}

# Quick'n Dirty
open FILE, '</etc/hosts';
map { print if /(?<!dr-)S+pm1/ } <FILE>;

I haven't tested it... perhaps it'll work.

(?<!pattern) is a zero width negative look behind
assertion. I.e.  
it'll match anything that isn't preceeded by the pattern
without  
including it in the match.

Garrett

On Oct 11, 2007, at 2:44 PM, Emmanuel Mejias wrote:

> trying to grep out some info from my /etc/hosts
file...just to get  
> more practice with Perl and well for my personal
pleasure, too.
>
> #!/usr/bin/perl
>
> open (FILE, "/etc/hosts");
> lines = <FILE>;
> close (FILE);
>
> foreach $line (lines){
>    if ($line =~ /pm1/){
>       print $line;
>    }
> }
>
> the problem with this is that it's printing out some
hosts that i  
> don't want that also have pm1 at the end.
>
> somehostpm1 but it also gets my dr hosts
(dr-somehostpm1)
>
> thoughts?
>
> by the way, i had to jump the wifes car so i ended up
missing out  
> on the meeting. gonna try to make the next one for
sure, though.
>
> _______________________________________________
> kc mailing list
> kcpm.org
> http://mail.pm
.org/mailman/listinfo/kc

_______________________________________________
kc mailing list
kcpm.org
http://mail.pm
.org/mailman/listinfo/kc

Re: Need second look at this...
user name
2007-10-11 15:58:25


On 10/11/07, djgoku < djgokugmail.com">djgokugmail.com> wrote:
On 10/11/07, Emmanuel Mejias < emmanuel.mejiasgmail.com">emmanuel.mejiasgmail.com> wrote:
&gt; trying to grep out some info from my /etc/hosts file...just to get more
> practice with Perl and well for my personal pleasure, too.
>
> #!/usr/bin/perl
>
> open (FILE, "/etc/hosts";);
> lines = <FILE&gt;;
> close (FILE);
&gt;
> foreach $line (lines){
&gt; &nbsp; &nbsp;if ($line =~ /pm1/){
&gt; &nbsp; &nbsp; &nbsp; print $line;
>; &nbsp; &nbsp;}
> }
>
>;
> the problem with this is that it's printing out some hosts that i don't want
>; that also have pm1 at the end.
>
> somehostpm1 but it also gets my dr hosts (dr-somehostpm1)
>
> thoughts?

Are the ones you don't want all have a prefix of 'dr-&#39;? Can we get a
sample list?
&nbsp;
correct!
cnxxpm1
cnxypm1
cnxzpm1
&nbsp;
dr-cnxxpm1
dr-cnxypm1
dr-cnxzpm1&nbsp;
dr-cnabpm1
dr-cncdpm1
 

> by the way, i had to jump the wifes car so i ended up missing out on the
> meeting. gonna try to make the next one for sure, though.
_______________________________________________
kc mailing list
kcpm.org">kcpm.org
http://mail.pm.org/mailman/listinfo/kc

[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )