1

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.

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.