Results 1 to 8 of 8

Thread: Need Perl help please.

  1. #1

    Need Perl help please.

    I need help with a script that pulls out everything in between the <Title> and </Title> in an XML document and puts it into a text file.

    Thanks for any help I can get.

  2. #2

    Re:Need Perl help please.

    okay i don't know how much you know or how much help you want...
    Code:
    # first you need to open the file:
    open(A, "<file.xml");
    #open file.xml for reading
    
    open(TITLES, ">>titles.txt");
    #open titles.txt for writting, in append mode as to not wipe it each time this is run.
    
    #now we want to read from it until we get an end of file, or a title tag
    while(<A>) {
       if(/<Title>(.*?)</Title>/i) {
       #ahh beautiful regex. i suggest running perldoc perlre so you can read more about it
       # but basicly this says search for <Title> and capture everything in between (.*?)
       # followed by a </Title>. and /i means ignore case
          print TITLES "$1\n";
          last;
          #break out of the loop, only one Title per file.
       }
    }
    i wrote that just now and didn't test it, but it looks good to me. let me know if you need more help, or help opening all files in a directory or something.

    good luck




  3. #3

    Re:Need Perl help please.

    Are you looking to make it work with the tages?

    Code:
    $ ./doit.pl filename output
    DONE!
    $ cat output
    TITLE: The Title
    AUTHOR: Mr. Franklin
    COMPANY: Evil Conglomerant International
    .....
    or just get it as is?

  4. #4

    Re:Need Perl help please.

    Hi guys, thanks for the help.

    Gorn:
    I tried what you wrote and I got this error:
    Code:
    Bareword found where operator expected at title_change.pl line 11, near "/<Title>(.*?)</Title"
            (Missing operator before Title?)
    Bareword found where operator expected at title_change.pl line 14, near "# followed by a </Title"
      (Might be a runaway multi-line // string starting on line 11)
            (Missing operator before Title?)
    syntax error at title_change.p
    l line 11, near "/<Title>(.*?)</Title"
    Search pattern not terminated at title_change.pl line 14.
    I have tried to resolve these problems but I am very very new to perl, any suggestions.

    Ashcrow:
    The XML file is composed of many HTML pages so the are multiple <Title> tags, what I wanted the script to do is pull out all of the titles and put them into a text file one per line.

    I am gonna keep trying to resolve the errors from the script that gorn gave but any other help that someone could give me would be appreciated.

  5. #5

    Re:Need Perl help please.

    Try this it's mainly gorns code modified a bit. It worked on a small variable set (three titles in XML). If it works I can explain it all to you if you want ....

    Code:
    #!/usr/bin/perl
    
    if( !@ARGV[1] ){
    
    print "\nUsage: pwrhouse inputfile outputfile\n\n";
    exit;
    }
    else {
    
    my $input = @ARGV[0];
    my $output = @ARGV[1];
    
    open (FILE, $input) or die $!;
    open (TITLES, ">>$output") or die $!;
    
    while(<FILE>) {
    if(/<Title>(.*?)<\/Title>/i) {
    print TITLES "$1\n";
    }
    }
    }

  6. #6

    Re:Need Perl help please.

    [quote author=Ashcrow link=board=9;threadid=6108;start=0#58082 date=1042851320]
    Code:
    if(/<Title>(.*?)<\/Title>/i) {
    [/quote]

    yeah thats what i messed up, forgot to escape the /

  7. #7

    Re:Need Perl help please.

    [quote author=gorn link=board=9;threadid=6108;start=0#58205 date=1042952064]
    yeah thats what i messed up, forgot to escape the /
    [/quote]

    I'd forget about it more often if it wasn't for 8) VIM 8)

  8. #8

    Re:Need Perl help please.

    Sorry it took so long to get back to you guys I was out of town.

    Anyways thanks alot for your help the script worked like a champ.

    You guys both get some karma.

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. Perl Help
    By pwrhouse in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 04-06-2003, 11:57 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
  •