Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2012, 01:24 PM
hcrosex3 hcrosex3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 52 hcrosex3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 19 m 15 sec
Reputation Power: 1
Pattern Matching Using Grep?

Currently my program will give me the list of just all NIS userids. The problem though is that it is also giving me accounts which are disabled. For those accounts the 1st character of the encrypted password is a “!”. When I run ypcat passwd | grep userid. How do I skip these accounts with out altering my current output?

PHP Code:
#Extracts  NIS Userids

my $allusers "";
my %users;
@
ypcat_cmd_users =(`ypcat passwd.byname | cut -f1 -d: | sort -n`);   
foreach 
my $line (@ypcat_cmd_users) {
    @
words split /\s+/, $line;
    
$username $words [0];
    
$users {$username} = 1;

}

#Runs df command on each user
    


foreach my $u (keys %users) {
    if ( 
$u eq "bb" || $u eq "tech1" || $u eq  "hpsmh" || $u eq "testact") {
    
$unext;
    }
    
    
    @
df_cmd = (`df -k ~$u`);

    foreach 
my $line (@df_cmd) {
    if (
$line =~ /^rusehf0b\.corp.utcitar\.us:\/.*\/home_(.*)/)  {
       
        
$users {$u} = 0;
                
}  
  
}

 } 

Reply With Quote
  #2  
Old June 28th, 2012, 03:56 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
I am not sure where in your code you have the encrypted password, but the basic regeps is as follows:

Code:
next if $password =~ /^!/;


which means: skip this record if $password starts with an exclamation mark.

Reply With Quote
  #3  
Old June 28th, 2012, 04:35 PM
hcrosex3 hcrosex3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 52 hcrosex3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 19 m 15 sec
Reputation Power: 1
Quote:
Originally Posted by Laurent_R
I am not sure where in your code you have the encrypted password, but the basic regeps is as follows:

Code:
next if $password =~ /^!/;


which means: skip this record if $password starts with an exclamation mark.



well when i run the ypcat command which you can see in the script i cut out the field 1 which is just the user id because thats what i want for my output. But if fields 2 has ! i want to skip it.

Reply With Quote
  #4  
Old June 29th, 2012, 02:25 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
If the second field is the one on which to filter on a leading "!", you could modify your code as follows:


perl Code:
Original - perl Code
  1. foreach my $line (@ypcat_cmd_users) {
  2.     @words = split /s+/, $line;
  3.     $username = $words [0];
  4.     next if $words[1] =~ /^!/;
  5.     $users {$username} = 1;
  6.     }


If the second field ($words[1]) starts with a "!", the $username entry will not even be created into your hash.

Reply With Quote
  #5  
Old June 29th, 2012, 05:20 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 5 sec
Reputation Power: 1170
If you don't mind, lets do a little code review which will solve your problem and help you write better code.

The first thing I noticed is that you're probably not using the strict pragma. I base that on some of your vars not being declared with the 'my' keyword. If you are using the strict pragma, then your code tells me that you're declaring some of those vars in the wrong place.
Code:
@ypcat_cmd_users =(`ypcat passwd.byname | cut -f1 -d: | sort -n`);
Lets look at this from right to left. Since you're loading the data into a hash, which is an unordered list, using sort in the command doesn't make any sense.

Using cut to select the desired fields is fine, but you're also using split in the foreach loop and doing both of those doesn't make any sense. My preference would be to drop the pipe to cut and handle it with split.

Code:
@words = split /s+/, $line;
$username = $words [0];
Did you rally mean to split on the literal 's' character? Or did you mean to split on \s whitespace? In either case, it doesn't make any sense in this case.

Var names should describe or convey some meaning about the contents of the var. @words doesn't tell you anything about the data it holds. The $username var does tell you what kind of data it holds and should have been used instead of the unneeded @words var.

Code:
foreach my $u (keys %users) {
A single letter var $u doesn't convey any meaning of the contents. You should use $user or $usr.

Code:
if ( $u eq "bb" || $u eq "tech1" || $u eq  "hpsmh" || $u eq "testact") {
Rather than using multiple alternation tests, it would be cleaner and more efficient to use a hash lookup for the usernames that you want to skip. The alternation approach doesn't scale well.

Code:
$u= next;
Doing that var assignment doesn't make any sense. Just have a simple next; statement.

Code:
@df_cmd = (`df -k ~$u`); 
The parens are unneeded.

Code:
if ($line =~ /^rusehf0b.corp.utcitar.us:/.*/home_(.*)/)  { 
You're using the wrong quantifier. You should be using + instead of *.

Your indentation in the code blocks is inconsistent.

Putting all that together gives you this:
Code:
#Extracts  NIS Userids

my %users;
my @nis_users = `ypcat passwd`;

foreach my $line (@nis_users) {
    my ($username, $pass) = split /:/, $line;
    next if $pass eq '!';
    $users{$username}++;
}
    
my %skip = (
  bb        => 1,
  tech1     => 1,
  hpsmh     => 1,
  testact   => 1,
);

# Run df command on each user
foreach my $user (keys %users) {
    next if exists $skip{$user};
    
    @df_cmd = `df -k ~$user`;

    foreach my $line (@df_cmd) {
        if ($line =~ /^rusehf0b.corp.utcitar.us:/.+/home_(.+)/)  {
            $users {$u} = 0;
        }  
  
    }
}
Comments on this post
Axweildr agrees: Nice answer - they take time

Reply With Quote
  #6  
Old June 29th, 2012, 06:12 PM
hcrosex3 hcrosex3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 52 hcrosex3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 19 m 15 sec
Reputation Power: 1
I do use the my stricts pragma. I define my varibles at the beginning of the program instead of having my all over. So I can keep track of what i've already used. But thank you for the other info I will def use some of those corrections to my script.

Reply With Quote
  #7  
Old June 29th, 2012, 07:02 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 5 sec
Reputation Power: 1170
Quote:
I define my varibles at the beginning of the program instead of having my all over
You really shouldn't do that. It's best to declare the vars in the smallest scope that they require and as close as possible to where they are needed.

Last edited by FishMonger : June 29th, 2012 at 07:17 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Pattern Matching Using Grep?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap