I'm going on some examples of pdbedit output I found online. Yours might differ; if you can provide some examples of what it prints, this code can be more tailored to what you need.
First, you need to find the usernames that match your prefix. It seems that pdbedit output is in username: foo format, so you could use awk to do that:
pdbedit -L -v | awk '/username:/ {print $2}'
Then we need to find names that start with your prefix, so let's use grep:
pdbedit -L -v | awk '/username:/ {print $2}' | grep ^eg
To see if a particular user's password needs to change, we can just see whether grep found that string, by checking its return code:
if pdbedit -v -u $name | grep -q "Password must change" ; then
echo $name
fi
Putting it all together with a loop that checks each name:
for name in $(pdbedit -L -v | awk '/username:/ {print $2}' | grep ^eg); do
if pdbedit -v -u $name | grep -q "Password must change" ; then
echo $name
fi
done
Update after you posted sample output
That format is clearly not designed by someone who's a fan of normal Unix command-line text processing tools! While you can, of course, do anything in any language, I'd probably move to something like Python or Perl to parse this one. I chose Perl here because Python's handling of dates and times is abysmal.
#!/usr/bin/perl
use Date::Parse;
$now = time();
while (<>) {
if (/Unix username:\s*(.*)/) {
$username = $1;
}
if (/Password must change:\s*(.*)/) {
$change = str2time($1);
if ($change <= $now) {
print "$username\n";
}
}
}
This will print the Unix username of any user whose password is expired. Use it like:
pdbedit -L -v | perl parse.pl
Edit by Question author
The final code I used was:
#!/usr/bin/perl
use Date::Parse;
$now = time();
while (<>) {
if (/Unix username:\s*(.*)/) {
$username = $1;
}
if (/Password must change:\s*(.*)/) {
$expiry = $1;
$change = str2time($expiry);
if ($expiry !~ /^never/ && $change <= $now && $username =~ /^eg[a-z]{3}\d+/) {
print "$username expiry: $expiry\n";
}
}
}
Which produces output like this:
egdfd001 expiry: Mon, 30 Jul 2012 08:12:02 NZST
egjpf001 expiry: Mon, 13 Aug 2012 07:50:03 NZST