Closed Thread
Results 1 to 5 of 5

Thread: Perl text parse help (noob)

  1. #1
    dfp
    dfp is offline Newbie
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Perl text parse help (noob)

    Hi,

    Just beginning perl and wanting to a perl script to do 2 things:

    First Pull a text file off a web site such as URL.com/sample.txt

    Then Text Parse the txt file to get figures for each line.

    The text file looks like this:
    username=DP
    data_down=8579.81 MB
    data_up=3894.36 MB
    free_data_down=22177.53 MB
    free_data_up=8311.49 MB

    I wanted to be able to return just the number for the query on each line. So If I ran the script "script.pl data_down" I would get just the figure 8579.81 printed.

    If possible can someone recommend or help me with some samples to achieve this with perl?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    fahlyn's Avatar
    fahlyn is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Thumbs down

    That's pretty simple, but first of all I've got a few questions. Is the location of the text file on the same or a different server than your script? if it is on the same server you should just be able to "open" it...otherwise you'll need to use LWP::Simple and use "get".

    Assuming that the file is on the same server you should be able to do something like this...(this is a CGI app...so instead of using CGI you'll just be able to use $ARGV[0] (that will contain the first argument passed to the script)

    Code:
    #!/usr/bin/perl
    use strict; use warnings;
    use CGI;
    my $cgi = CGI->new;
    
    open(FILE, "data.txt") || die("Could not open file!");
    my @contents=<FILE>;
    close(FILE);
    
    my $argv = $cgi->param('argv');
    
    print "Content-type:text/plain\n\n";
    foreach my $content (@contents){
        if($content =~ m/^$argv=(.+)\sMB/){
            print "$1\n";
       }
    }
    That should do what you need.
    Visit My Google Group Here: Web Development Innovation

  4. #3
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    He's not running a CGI script. He was also advised on another forum where to look to start coding such a script. LWP::Simple to fetch the page then parse it using the split() function.

  5. #4
    fahlyn's Avatar
    fahlyn is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0
    Quote Originally Posted by fahlyn View Post
    you'll need to use LWP::Simple and use "get". ...so instead of using CGI you'll just be able to use $ARGV[0]
    I realize dfp is not writing a CGI script...that is why i made the comment above....I also mentioned LWP::Simple...however I think that using a regex is quite a bit easier than the split function...if you're using the split function and splitting by an "=" then you'll still need to get rid of the " MB" at the end of each record (see dfp's example)
    Visit My Google Group Here: Web Development Innovation

  6. #5
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    Quote Originally Posted by fahlyn View Post
    I realize dfp is not writing a CGI script...that is why i made the comment above....I also mentioned LWP::Simple...however I think that using a regex is quite a bit easier than the split function...if you're using the split function and splitting by an "=" then you'll still need to get rid of the " MB" at the end of each record (see dfp's example)
    My bad. I didn't read all of your comments. Sorry.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. I can't parse an XML file
    By Alhazred in forum PHP Development
    Replies: 4
    Last Post: 11-23-2010, 09:16 AM
  2. Does anyone know how to parse an address from a text field?
    By TheWebGuy in forum ASP, ASP.NET and Coldfusion
    Replies: 2
    Last Post: 11-09-2010, 02:50 AM
  3. PHP Parse Error
    By crd06c in forum PHP Development
    Replies: 3
    Last Post: 08-23-2009, 07:23 AM

Tags for this Thread

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