www.webdeveloper.com
Results 1 to 5 of 5

Thread: reading from file and outputting regex matches?

  1. #1
    Join Date
    Sep 2006
    Posts
    15

    reading from file and outputting regex matches?

    The goal is to read the code in the file being opened and output only the text strings that match the regular expression of /<b>anything</b>/ to a new file.

    Here is the current code:
    ____________________________________________________________

    #!/usr/bin/perl

    open(SOURCE, "file") || die("can't open file: $!");

    @array = <SOURCE>;

    close SOURCE;

    foreach $line (@array) {

    print $line;

    } # End of foreach.

    __________________________________________________________

    Thanks for your help,

    -vbsaltydog

  2. #2
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    So where do you need help?
    Seems like you got most of it. Just do a little more studying.

    Code:
    #!/usr/bin/perl
    
    use strict;
    
    ## open filename1 for reading
    open(SOURCE, "file") || die("can't open file: $!");
    
    ## open filename2 for writing
    open(DESTIN, "file") || die("can't open file: $!");
    
    while (<SOURCE>) {
      chomp; ## remove EOL char
      if (regex test valid) {
        print DESTIN "$_\n";	
      }
    }
    
    close DESTIN;
    close SOURCE;

  3. #3
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    Oops, you forgot "write mode"...
    Code:
    ## open filename2 for writing
    open(DESTIN, ">file") || die("can't open file: $!");

  4. #4
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Quote Originally Posted by CyCo
    Oops, you forgot "write mode"...
    Actually, I didn't.
    I was leaving that as an exercise for vbsaltydog
    The script I posted is VERY incomplete. Just a 'here's how you might do it'.

  5. #5
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    my bad...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles