Reputation: 11
I'm running a tf2 server, and I have a simple Perl script I found that pulls back the stats of it. I need to edit the formatting of it so that cacti can read it nice and proper. The script is this:
use Rcon::HL2;
my $rcon = Rcon::HL2->new(
hostname => "myserverhere",
password => "omgawesomepassword",
);
When I run it, it returns:
0CPU In Out Uptime Users FPS Players
0.00 0.00 0.00 514 9 956.02 0
Initially, I'd like for it to just output the numbers - 0.00 0.00 0.00 514 9 956.02 0
, perhaps separated by a tab or comma.
Can anyone help me out with this?
I was looking around and messing with cut
and sed
and such but can't seem to find the right way for me to get it done. Ideally, I'd like to be able to run myscript.perl -cpu
, or myscript.pl -in
, etc., to return just those bits. I think if someone can show me how to manipulate the output, I can figure out the rest.
Upvotes: 1
Views: 221
Reputation: 62236
You can use the substitution operator (s///) to replace all whitespace with commas, for example:
use warnings;
use strict;
my $out = '0.00 0.00 0.00 514 9 956.02 0';
$out =~ s/\s+/,/g;
print "$out\n";
Output:
0.00,0.00,0.00,514,9,956.02,0
Upvotes: 3
Reputation: 1612
It sounds like you are trying to change a modules output format. The best way to do that is to call it from another script, so you can get it as input, then modify it as you wish. Perl has output buffering but it should probably be called output caching because it doesn't really allow you to modify the stream once it has been created.
Ex. - Inside rconhl2.pl
use Rcon::HL2;
my $rcon = Rcon::HL2->new(
hostname => $ARGV[0],
password => $ARGV[1],
);
...
Ex. - Inside myscript.pl
...
$var = `perl rconhl2.pl $param1 $param2`;
$var =~ s/s+/,/;
...
Something along those lines. Any module that doesnt return the string is frustrating when you need to meet a spec. Good luck though.
Upvotes: 0