RCP has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to PERL. I am familiar with UNIX shell (self taught) programming. I would like some pointers as to how to wite a PERL script to do the following: 1. Read a file (cat filename) 2. Select specific sentences from the file: grep -i name 3. Read specific columns in those sentences: "while read d d line1 d line2 d" 4. Print out that information as I want (EXAMPLE): print "see how $line1 compares to $line2"

Any help would apreciated...

Thanks... Rich

janitored by ybiC: Retitle from one word "RCP" for searchability, minor format tweak for legibility

Replies are listed 'Best First'.
Re: RCP
by ambrus (Abbot) on Feb 17, 2004 at 19:38 UTC

    I won't give you examples, just an advice that may or may not work. You probably already know how to do these things with awk. Write an awk script and translate it to perl with a2p. A2p is not failproof but it will work in these simple cases I belive.

Re: Read and parse text file
by waswas-fng (Curate) on Feb 17, 2004 at 21:58 UTC
    #!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 } }
    this is similar to the unix commands:
    grep -i root /etc/passwd | awk '{ ... You can figure it out.


    -Waswas
      Thanks for the short code, it's exaclly what I was looking for. My unix scripts finds specific files generated by a software's report, extracts specific data from columns and writes out a "macros" to execute within the software. I was supposed to start PERL class next week, but heart surgery cut that short, now I have something to do while I'm at home recuperating. Thanks! RCP
      #!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
        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
Re: Read and parse text file
by ysth (Canon) on Feb 17, 2004 at 20:10 UTC
    You might start by looking at the -n, -p, and -a switches, documented in perldoc perlrun (or man perlrun).

    In the vaporware-answer category, Manning Publications has a book coming out soon, "Minimal Perl for Shell Users and Programmers".