- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: search pattern issue
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 02:44 AM
тАО09-24-2010 02:44 AM
Re: search pattern issue
what exactly means that {0} in $pattern{0} ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 03:58 AM
тАО09-24-2010 03:58 AM
Re: search pattern issue
http://www.perlmonks.org/?node_id=967
Best regards,
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 04:09 AM
тАО09-24-2010 04:09 AM
Re: search pattern issue
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 05:43 AM
тАО09-24-2010 05:43 AM
Re: search pattern issue
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 06:12 AM
тАО09-24-2010 06:12 AM
Re: search pattern issue
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-26-2010 09:47 PM
тАО09-26-2010 09:47 PM
Re: search pattern issue
print "$user\n" unless exists $exclude{$user};
}
1;
Hi JRF,
What is this "1;" for ?
Regards,
Hakki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2010 04:10 AM
тАО09-27-2010 04:10 AM
Re: search pattern issue
> 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...
- « Previous
- Next »