Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl

Status
Not open for further replies.

help911

Technical User
Aug 31, 2003
7
0
0
US
How do you write a script that looks through the /etc/passwd
file for two or more users with the same first name and prints those names. Here is what I have so far and it don't seem to work.Can some tell me what I am doing wrong here thanks.


my $f = '/etc/password';
open(F,$f) || die "Can't open passwd file\n$!\n";
while(<F>){
($uname,$password,$uid,$gid,$username,$home,$shell) = split(/:/,$_);
($firstname,$temp)= split(/\s+/$username);
}#end of while
 
my $f = &quot;/etc/password&quot;;
open(F,$f) || die &quot;Can't open passwd file\n$!\n&quot;;
while(<F>){
($uname,$password,$uid,$gid,$username,$home,$shell) = split(/:/,$_);
($firstname,$temp)= split(/\s+/$username);
}#end of while

Up to here you are right but how are you plannig to compare your first rows name with second and third and so?
You can do something like this.
my $f1 = &quot;/etc/password&quot;;
my $f2 = &quot;/etc/password&quot;;
open(F1,$f1) || die &quot;Can't open passwd file\n$!\n&quot;;
open(F2,$f2) || die &quot;Can't open passwd file\n$!\n&quot;;
while(<F1>)
{
($uname1,$password1,$uid1,$gid1,$username1,$home1,$shell1)
= split(/:/,$_);
($firstname1,$temp)= split(/\s+/$username1);
// Now you have first name from first row.
while newline (<F2>)
{
($tmp1, $tmp1,$tmp1,$tmp1,$username2,$tmp1)=split(/:/,$newline);
($firstname2,$tmp1)=split(/\s+/,$username2);
//now you have firstname from second row.
//compare them useing if statment
if ($firstname1 == $firstname2)
{ print $firstname1; }
} //end of while f2
} //end of while f1


Sachin


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top