Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: traverse through passwd file, and split fields to extract UID/GID

by kcott (Archbishop)
on Apr 23, 2014 at 12:06 UTC ( [id://1083338]=note: print w/replies, xml ) Need Help??


in reply to traverse through passwd file, and split fields to extract UID/GID

G'day intoperl,

Welcome to the monastery.

There's builtin functions to do this. No need to reinvent this particular wheel. Try this from your command line:

$ perldoc -f getpwent

-- Ken

  • Comment on Re: traverse through passwd file, and split fields to extract UID/GID
  • Download Code

Replies are listed 'Best First'.
Re^2: traverse through passwd file, and split fields to extract UID/GID
by intoperl (Acolyte) on Apr 23, 2014 at 13:15 UTC
    Thanks Ken, for bringing up getpwent. BTW, i used the other approach which did not mention getpwent, but still the job was done, by the code below :
    my $HAN = "handle"; my $FILE = "passwd.txt"; open("$HAN", "$FILE")||die "Error opening file: $!\n"; while(<$HAN>) { my ($name,$passwd,$uid,$gid,$gcos,$dir,$shell) = split(/:/,$_); print "UID is: $uid\n"; print "GID is: $gid\n"; } close("$HAN");
      "Thanks Ken, for bringing up getpwent. BTW, i used the other approach which did not mention getpwent, but still the job was done, ..."

      You're welcome. I'm glad you found a working solution.

      For future reference, all of that code could have been replaced by:

      while (my @pw = getpwent) { printf "UID is: %s\nGID is: %s\n", @pw[2,3] }

      By the way, take a look at open for better ways of doing: open("$HAN", "$FILE")

      -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083338]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found