Results 1 to 5 of 5

Thread: Perl Help

  1. #1

    Perl Help

    I have been trying to write a script that takes a file with a whole bunch of HTML pages in it as input, and prints out everything in between the <body and </body> tags to a new file.
    Here is what I have so far:
    Code:
    #!perl -w
    use strict;
    use warnings;
    
    open(A, "<all-files.html");
    
    open(BODY, ">>body.txt");
    
    while(<A>) {
    if(/<body(.*?)<\/body>/i) {
          print BODY "$1\n";
      }
    }
    The script runs but all I get is an empty file as output. I think the problem is that the body tags are on different lines but I have no clue how to address this.

    Any help would be appreciated.

  2. #2

    Re:Perl Help

    This is horrible code but it works ....

    Code:
    #/usr/bin/perl -w
    use warnings;
    
    $test = 0;
    
    open(A, "<a.html");
    
    open(BODY, ">>body.txt");
    
    
    while(<A>) {
            if(/<body>/ and !$test) {
                    $test = 1;
                    last;
            }
    }
    
    while(<A>) {
            if( !(/<body>/) && !(/<\/body>/) && $test == 1) {
                    print BODY $_;
            }
    }
    
    close BODY;
    close A;

  3. #3

    Re:Perl Help

    Code:
    #!/usr/bin/perl -w
    open IN, "<foo.html";
    open OUT, ">out.txt";
    undef $/; #this lets us read a whole file in one "slurp";
    
    $in = <IN>; #$in contains the whole foo.html file now
    
    while($in =~ s/<body>(.*?)<\/body>//s) {
    # so we pull our body's out of $in, eventually $in contains no more and we can quit. the /s at the end of that means treat like a single line, meaning ignore \n's
    print OUT "$1\n";
    }
    i didn't test it but that should do.

  4. #4

    Re:Perl Help

    Don't forget to close the files :P

  5. #5

    Re:Perl Help

    [quote author=Ashcrow link=board=9;threadid=6714;start=0#63422 date=1049581780]
    Don't forget to close the files :P
    [/quote]

    it doesn't matter unless you keep the program running, so if this was in a loop, yeah you'd want to close them. but if it just exits it doesn't matter. though i guess it's cleaner code to close them.

Similar Threads

  1. PHP & Perl??
    By SwampDonkey in forum Linux - Software, Applications & Programming
    Replies: 8
    Last Post: 07-25-2004, 04:12 PM
  2. CGI, SSI, Perl
    By vwgtiturbo in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 10-29-2003, 06:05 PM
  3. Need Perl help please.
    By pwrhouse in forum Linux - Software, Applications & Programming
    Replies: 7
    Last Post: 01-20-2003, 01:49 PM
  4. perl question from a perl newb
    By tolstoy in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 09-06-2002, 01:16 AM
  5. perl @$foo{'bar'}
    By gorn in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 01-04-2002, 02:27 AM

Bookmarks

Posting Permissions

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