I am using the following Perl script to determine the last logon of all users in /etc/passwd. I am VERY new to Perl, and I need to limit the search, however, to users that have a similar word (string) in their home directory. I am more POSIX shell savvy, but this is what I kinda am looking for:
grep (string) /etc/passwd
Using this file, is there somewhere I could grep for this (string) instead of sourcing in the entire /etc/passwd file?
#!/usr/local/bin/perl
# Show last logins for every user in /etc/passwd
#
open(LASTLOGIN,"last|") || die "Cannot run 'last' command\n";
while (<LASTLOGIN>) {
next if $_ eq "";
if (/^wtmp begin/) {$begin=$_ ; next};
($name,$tty,$date)=/(\S+)\s+(\S+)\s+(\S+\s+\S+\s+\S+)/;
$user{$name}=$date unless $user{$name};
}
print $begin;
close(LASTLOGIN);
open(PASSWD,"/etc/passwd") || die "Cannot open '/etc/passwd'\n";
while (<PASSWD>) {
next if /^\+/;
($name,$rest)=split(":");
$user{$name}="**No Logon Since /etc/wtmp File Created**" unless $user{
+$name};
}
foreach $name (sort keys(%user)) {
printf "%-10s %s\n",$name,$user{$name};
}
2004-10-19 Edited by Arunbear: Changed title from 'Easy one for the Monks !'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.