[Kc] Need second look at this...
Garrett Goebel
ggoebel at goebel.ws
Thu Oct 11 13:40:17 PDT 2007
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
> kc at pm.org
> http://mail.pm.org/mailman/listinfo/kc
More information about the kc
mailing list