- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- 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
Forums
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
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-23-2010 10:01 PM
тАО09-23-2010 10:01 PM
@users=`cat /etc/passwd|awk -F: '{print \$1}'`;
my $K;
for ( $K = 1; $K <= @users; $K++ )
{
open(IN,"/tmp/UserLogin");
open(OUT,">>/tmp/LogonOutput");
while ($line=
$pattern = "$users[$K-1]" ;
if ( $line =~ m{$pattern} ) {
print "$pattern equal to $line\n";
print OUT "$line\n";
}
}
}
Besides,
I tried m/\b$pattern/ also ,but it did not work at all.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 10:27 PM
тАО09-23-2010 10:27 PM
Re: search pattern issue
Why m/ ? (Multiple lines?) you only read one line at at a time, so should use s/
Best regards
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 10:30 PM
тАО09-23-2010 10:30 PM
Re: search pattern issue
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 10:40 PM
тАО09-23-2010 10:40 PM
Re: search pattern issue
m/ stand for match , but seems useless somehow here, I tried without m like this:
if ( $line =~ /\b$line/ )
it worked now.
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 10:41 PM
тАО09-23-2010 10:41 PM
Re: search pattern issue
Move
open(IN,"/tmp/UserLogin");
open(OUT,">>/tmp/LogonOutput");
outside for ! You actually read the same data a lot of times...
Horai
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 10:42 PM
тАО09-23-2010 10:42 PM
Re: search pattern issue
close(IN)
close(OUT)
Best regards,
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 10:50 PM
тАО09-23-2010 10:50 PM
Re: search pattern issue
Meaning $line was searched for $line?
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2010 11:01 PM
тАО09-23-2010 11:01 PM
Re: search pattern issue
Meaning $line was searched for $line?
Right, it is mistake, I just noticed that put OPEN File handler out of for loop, but I am still unable to pick from file what I wanted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:08 AM
тАО09-24-2010 12:08 AM
Re: search pattern issue
($line =~ m/$pattern{0}/ )
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:10 AM
тАО09-24-2010 12:10 AM
Re: search pattern issue
my $K;
#open(IN,"/tmp/UserLogin");
open(OUT,">>/tmp/LogonOutput");
for ( $K = 1; $K <= @users; $K++ )
{
open(IN,"/tmp/UserLogin");
while ($line=
{
$pattern = "$users[$K-1]" ;
if ( $line =~ m/$pattern{0}/ ) {
print "$pattern equal to $line\n";
print OUT "$line\n";
}
}
close(IN);
}
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:26 AM
тАО09-24-2010 12:26 AM
Re: search pattern issue
only the first pattern working, others not working, problem is why perl picks up all pattern they are not even on the $pattern values ?
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:35 AM
тАО09-24-2010 12:35 AM
Re: search pattern issue
Thanks, the problem solved apparently now, but another problem ,if patterns don t match , still written into OUTPUT file ??
is it weird ? something is missing in the loop ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:42 AM
тАО09-24-2010 12:42 AM
Re: search pattern issue
What exactly do you get in the output file in this case?
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:45 AM
тАО09-24-2010 12:45 AM
Re: search pattern issue
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 12:48 AM
тАО09-24-2010 12:48 AM
Re: search pattern issue
@users=`cat /etc/passwd|awk -F: '{print \$1}'`;
my $K;
open(OUT,">>/tmp/LogonOutput");
for ( $K = 1; $K <= @users; $K++ )
{
open(IN,"/tmp/UserLogin");
while ($line=
{
$pattern = "$users[$K-1]" ;
if ( $line =~ m/$pattern{0}/ ) {
print "$pattern equal to $line\n";
print OUT "$line\n";
}
}
close(IN);
}
close(OUT);
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 01:08 AM
тАО09-24-2010 01:08 AM
Re: search pattern issue
What exactly do you get in the output file in this case?
this is from output file:
LOGIN Sep 17 2010 18:03:09
root Sep 17 2010 18:03:24
student Sep 20 2010 13:55:45
LOGIN and root are not in the @users list but goes to output file ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 01:14 AM
тАО09-24-2010 01:14 AM
Re: search pattern issue
I tried to change environment and seems its working now , so my test server has some problems by Perl sources ??
I will notify the solution after completed tests.
Thanks
Hakki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 01:17 AM
тАО09-24-2010 01:17 AM
SolutionThis could be true also for the "student" user.
Looks strange the presence of LOGIN.
grep LOGIN /etc/passwd?
Can you attach the file /tmp/UserLogin ?
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 01:19 AM
тАО09-24-2010 01:19 AM
Re: search pattern issue
What exactly did you changed in the env. ?
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2010 02:34 AM
тАО09-24-2010 02:34 AM
Re: search pattern issue
No, I took it out from passwd file with "grep -v"
I meant another server with environment change.
Last script is here (working great now!)
@users=`cat /etc/passwd|awk -F: '{print \$1}'|grep -vE 'root|nwauto|daemon|bin|sys|adm|uucp|lp|www|webadmi
n|hpdb|informix|nuucp|smbnull|mysql|nwcron|goglobal|alop|trop|dbadmin|c7op|sysadmin`;
my $K;
open(OUT,">>/tmp/LogonOutput");
for ( $K = 1; $K <= @users; $K++ )
{
open(IN,"/tmp/UserLogin");
while ($line=
{
$pattern = "$users[$K-1]" ;
if ( $line =~ m/\b$pattern{0}/ ) {
print "$pattern equal to $line\n";
print OUT "$line";
}
}
close(IN);
}
close(OUT);
- 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...