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?
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)
That should do what you need.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"; } }
Visit My Google Group Here: Web Development Innovation
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.
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks