home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5680 < prev    next >
Encoding:
Text File  |  1992-09-02  |  1.6 KB  |  58 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!fmrco!fmrco!asherman
  3. From: asherman@laser.fmrco.com (Aaron Sherman)
  4. Subject: Re: how to get passwd
  5. In-Reply-To: tony@hacgate.SCG.HAC.COM's message of 31 Aug 92 23:39:53 GMT
  6. Message-ID: <ASHERMAN.92Sep2110720@laser.fmrco.com>
  7. Sender: news@fmrco.uucp
  8. Reply-To: asherman@fmrco.COM
  9. Organization: I-Kinetics, 19 Bishop-Allen Dr., Cambridge, MA
  10. References: <23093@hacgate.SCG.HAC.COM>
  11. Distribution: usa
  12. Date: Wed, 2 Sep 1992 16:07:20 GMT
  13. Lines: 43
  14.  
  15.  
  16. >>>>> tony@hacgate.SCG.HAC.COM (Tony Reeves) said:
  17.  
  18. tony> I'm new to perl, so I'm sorry if this is a dumb question...
  19.  
  20. There are no such things as dumb questions.
  21.  
  22. tony> I want to read the last line of /etc/passwd and grep out the uid
  23. tony> number.  I can't seem to do this. How can I do this easy? I
  24. tony> tryed open, seek and split.  But it does not seem to work the
  25. tony> way I read it in the man page. Can a perl expert help me?
  26.  
  27. That has to be the dumbest question I've ever heard in my life! ;-)
  28.  
  29. How about:
  30.  
  31.     $\="\n";
  32.     open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");
  33.     $last = $_ while(<F>);
  34.     print((split(/:/,$last))[2]+0);
  35.  
  36. or
  37.  
  38.     $\="\n";
  39.     open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");
  40.  
  41.     seek(F,-100,2);
  42.       FIND:
  43.     for($i=0;defined($c=getc(F)) && $c ne "\n" && $i < 100;$i++){1;}
  44.     if ($c ne "\n") {seek(F,-200,1);goto FIND;}
  45.  
  46.     $last = $_ while(<F>);
  47.     print((split(/:/,$last))[2]+0);
  48.  
  49.  
  50.  
  51.             -AJS
  52.  
  53. --
  54. --------
  55. Disclaimer: I am solely responsible for the content of this message.
  56. The views expressed here may not be the views of I-Kinetics, Fidelity,
  57. any of the Fidelity-owned corporations or my mother.
  58.