1748252 Members
3792 Online
108760 Solutions
New Discussion юеВ

Re: search pattern issue

 
SOLVED
Go to solution
Hakki Aydin Ucar
Honored Contributor

Re: search pattern issue

>Horial: if ( $line =~ m/$pattern{0}/ )

what exactly means that {0} in $pattern{0} ?
Horia Chirculescu
Honored Contributor

Re: search pattern issue

Read about quantifiers in (and search google for more):

http://www.perlmonks.org/?node_id=967

Best regards,
Horia.
Best regards from Romania,
Horia.
James R. Ferguson
Acclaimed Contributor

Re: search pattern issue

Hi Hakki:

There are a number of things wrong in your script.

First, you count starting an an index of one. Perl is zero relative. Your 'for' loop thus skips the first entry in the '/etc/passwd' file and attempts to process a non-existent one when it compares '$K<='.

Contrast your code to this:

#!/usr/bin/perl
use strict;
use warnings;
my ( @users, $K );
@users=`cat /etc/passwd|awk -F: '{print \$1}'`;
for ( $K = 0; $K < @users; $K++ ) {
print $users[$K];
}

...

Next, the above is horribly un-Perlish. You start a external process to fill an array and then immediately fork a 'cat' and an 'awk' when the 'awk' would do alone (if this were a shell program). There is *no* need for any of this in a Perl script.

Instead do something like:

#!/usr/bin/perl
use strict;
use warnings;
my $fh;
my $file = '/etc/passwd';
my @users;
open( $fh, '<', $file ) or die "Can't open '$file': $!\n";
while (<$fh>) {
push @users, (split /:/) [0];
}
for my $user (@users) {
print "$user\n";
}

I would urge you to use the three-argument form of 'open()' too.

Regards!

...JRF...
Hakki Aydin Ucar
Honored Contributor

Re: search pattern issue

>JRF:

Great and very Perlish solution :) Thanks.
What method do you recommend to get rid of unwanted system users like grep -v is for All lines but those matching are printed ?

All lines but those matching are printed.

@users=`cat /etc/passwd|awk -F: '{print \$1}'|grep -vE 'root|nwauto|daemon|bin|sys|adm|uucp|lp|www|we
badmin|hpdb|informix|nuucp|smbnull|mysql|nwcron|goglobal|alop|trop|dbadmin|c7op|sysadmin|ftp`;

Best Regards,
Hakki
James R. Ferguson
Acclaimed Contributor

Re: search pattern issue

Hi (again) Hakki:

> What method do you recommend to get rid of unwanted system users

One way is to create a hash of exclusions. Then, skip the processing of any user that exists in the hash:

# cat ./dousers
#!/usr/bin/perl
use strict;
use warnings;
my $fh;
my $file = '/etc/passwd';
my @users;
my %exclude;
@exclude{qw( root daemon bin sys adm uucp lp nuucp hpdb nobody www)} = ();
open( $fh, '<', $file ) or die "Can't open '$file': $!\n";
while (<$fh>) {
push @users, ( split /:/ )[0];
}
for my $user (@users) {
print "$user\n" unless exists $exclude{$user};
}
1;

Regards!

...JRF...
Hakki Aydin Ucar
Honored Contributor

Re: search pattern issue

>JRF:for my $user (@users) {
print "$user\n" unless exists $exclude{$user};
}
1;

Hi JRF,
What is this "1;" for ?

Regards,
Hakki
James R. Ferguson
Acclaimed Contributor

Re: search pattern issue

Hi (again) Hakki:

> What is this "1;" for ?

I use the terminal '1;' to signify the end of a script. If we were writing a Perl *module*, the return value of 'true' would be required to tell the compiler that the module was fully parsed and successfully loaded. Obviously, the code I provided isn't a module, but I have adopted the notation to denote end-of-code.

Regards!

...JRF...