View Full Version : pipe data into script (non-HTML)
dillieo
08-30-2002, 05:42 PM
Hey all,
This isn't an HTML based question, but is there a way to essentially pipe data into a perl script and then grab that data, through a variable or read buffer command? Essentially I want an existing program to pipe a string to a perl script, which would grab the string, stick a timestamp on it, and then dump it to a file.
In other words. I need to fill in the blank...
vmstat 3600 | perl myfunc.pl <data to go here>
then in my perl function I'd need at the beginning
#!/usr/bin/perl -w
<command to grab data from command line>
Make sense?
Please let me know if you have any ideas. Thanks!
Jeepers
08-30-2002, 11:58 PM
I'm sure somebody will correct me if I'm wrong, but here goes.
perl myfunc.pl?data that you want goes here
to grab the passed date
$datain = $ENV{'QUERY_STRING'};
to date and time stamp it
sub get_date {
@months = ('January','February','March','April','May','June','July','August','September','October','November', 'December');
@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$month = sprintf("%.2d", ($mon + 1));
$mday = sprintf("%.2d", $mday);
$year += 1900;
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$date = "$days[$wday], $months[$mon] $mday, $year at $time";
$date;
}
$date = &get_date;
$datain .= " [received: $date]";
then dump to file (appending data)
open(FILE, ">>absolute path of file to dump to");
# flock(FILE, 2); # if UNIX server
# binmode(FILE); # if win32 server
seek(FILE, 0, 2);
print FILE "$datain\n";
close(FILE);
If not that then somat like it should do it. I'm only learning myself so please forgive if this does not work. Good luck
Mouldy_Goat
08-31-2002, 04:05 PM
When you pipe information from one command to another, the second receives it via STDIN.
So here you could just use the following:
#!/usr/bin/perl -w
use strict;
my $file = "dumpfile.txt";
chomp(my $line = <STDIN>);
open (DUMP, ">>$file") || die "Couldn't open $file for appending: $!";
print DUMP "[",scalar localtime,"] $line\n";
close DUMP;
print "Information dumped to $file\n";
By the way, a convenient way of doing a timestamp is to just use the localtime function in a scalar context - here I've forced it into one with the scalar function.
The output is something like this:
Sat Aug 31 16:00:11 2002
Hope that helps.
Just a couple of notes on Jeeper's reply, it would've been great for a CGI script, but I don't think this is what dillieo is after (correct me if I'm wrong..).
The file-locking is a good idea no matter what system you're on, I don't use it most of the time because I'm fairly certain that there won't be multiple accesses to my datafiles and because I'm lazy in a bad way.
The binmode function is needed only for Windows systems, but only when you're writing/reading binary data, which isn't the case here.
Also, I'm not actually entirely sure what the seek you've used there is for, as in I don't actually know what it does here, if you could explain I'd be grateful.
From what I know it looks as though it just doesn't actually move the file pointer at all, from the end of the file.
KevinADC
08-25-2009, 04:39 PM
This might be the grand prize, posting to a 7 year old thread.
grace_callaghan
08-27-2009, 07:12 AM
hmm.. I got the same error, but after deleting "weather.xml" in the "data" folder, it works. I made a typo and this led to a wrong .xml-file. Also this is really good read for me. I like your blog because of its interesting topics you post. i'm student and these days i am preparing for my exam of testking 70-291 (http://www.real-testking.com/exam/70-291.htm) for attaining the certification. Although it is not hard to get certified but it keeps me busy. I've also attained certification of another course of testking 70-270 (http://www.real-testking.com/exam/70-270.htm) which I had passed last three months back. Also I am trying to complete my pending thesis project of testking E20-361 (http://www.real-testking.com/exam/E20-361.htm) which I'm sure will be completed soon. I normally search on topics which you described on your blog these are great and really informative. Thanks for posting this informative.
grace_callaghan
08-27-2009, 07:16 AM
Now I know exactly what you mean, that was very helpful!!
Thanks a million!! I'll go re-check the whole thing.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.