in reply to Re: Read and parse text file
in thread Read and parse text file

#!perl open (FILEH, "/etc/passwd"); #open the file for read while (<FILEH>) { #while there are still lines in FILEH... chomp; #clear newlines... if ( /root/i ) { # if line contains "root" case insensitive` ($username,$id) = (split /:/)[0,2]; #pull out field one and th +ree print "User: $username, ID: $id\n"; #print junk } }
Thanks again for your help earlier. I was wondering if there was a way to have this script read each line in a file and if there is not a fourth column, do not write that line out. If there is a fourth column, then write the line out. Thanks, RCP

Replies are listed 'Best First'.
Re^3: Read and parse text file
by Roy Johnson (Monsignor) on Mar 08, 2004 at 18:28 UTC
    If you're splitting on ':',
    while (<>) { print if tr/:// >= 3; # Special use of tr to count column separato +rs }
    To count columns more literally, and splitting on the more traditional whitespace:
    while (<>) { my @cols = split; print if @cols >= 4; }

    The PerlMonk tr/// Advocate
      Your second approach worked much better! Thanks again... RCP