DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Parsing Unix /etc/passwd File

05.04.2006
| 8570 views |
  • submit to reddit
        // Here is an easy way to parse the /etc/passwd file.  Perl has built in
functions that only use /etc/passwd as a file.  It's built into the
function and you don't have to declare it.  

#!/usr/bin/perl -w

setpwent();
while (@list = getpwent())  {
     ($LOGIN,$PASSWORD,$UID,$GID,$QUOTA,$COMMENT,$GECOS,$HOMEDIR,$SHELL)
= @list[0,1,2,3,4,5,6,7,8];
    print "$LOGIN,$PASSWORD,$UID,$GID,$QUOTA,$COMMENT,$GECOS,$HOMEDIR,$SHELL\n";
                            }
endpwent();