1835177 Members
2510 Online
110077 Solutions
New Discussion

Perl mongers

 
SOLVED
Go to solution
David_246
Trusted Contributor

Perl mongers

Hi Perl freaks,

I know it can be done faster, any suggestions ?

#!/usr/local/bin/perl

#$load = `ssh 1n2 uptime`;
$load = " 9:45am up 88 days, 10:42, 9 users, load average: 0.26, 0.13, 0.13"
@load = split(/,/, $load);
$load = $load[3];
@load = split(/:/, $load);
$load = $load[1];
$load =~ s/\s+//g;

print "$load \n";

I do it this way, as I cannont get the following to work :

$load = `ssh 1n2 uptime | awk -F, '{print $3}' | awk -F: '{print $2}'`;
or
$load = `ssh 1n2 "uptime | awk -F, '{print $3}' | awk -F: '{print $2}'"`;
or
$load = `ssh 1n2 "uptime" | awk -F, '{print $3}' | awk -F: '{print $2}`;

Anyone has an idea ?

Regs David
@yourservice
6 REPLIES 6
Ollie R
Respected Contributor

Re: Perl mongers

Hi David,
The "ssh" command returns nothing because the first "awk" pipe is incorrect - should br $4, not $3.
Try:
$load = `ssh 1n2 uptime | awk -F, '{print $4}' | awk -F: '{print $2}'`;

This should work fine.

Ollie.
To err is human but to not award points is unforgivable
David_246
Trusted Contributor

Re: Perl mongers

Hi,

You're awake :)
Although it's still not working :

# ./tst.pl
10:36am up 88 days, 11:33, 9 users, load average: 0.20, 0.27, 0.21

The problem is that the pipe does not work inside the quotes. I can imagine why, just not how to get around it.

Regs David
@yourservice
Ollie R
Respected Contributor
Solution

Re: Perl mongers

Hi David,
Sorry, I'm half asleep!
You need to escape the '$' characters as they are interpolated in the string before being sent to the shell:

$load = `ssh 1n2 uptime | awk -F, '{print \$3}' | awk -F: '{print \$2}'`;

Hope that's better!?

Ollie.
To err is human but to not award points is unforgivable
H.Merijn Brand (procura
Honored Contributor

Re: Perl mongers

Why the .... use awk inside perl?

$load = `ssh 1n2 uptime | awk -F, '{print \$3}' | awk -F: '{print \$2}'`;

=>

$load = (split /:/, (split /,/, `ssh 1n2 uptime`)[2])[1];

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
David_246
Trusted Contributor

Re: Perl mongers

You must be the best !!!
Muchios gratias.

You solved quiet some issues for me right now.


Best regs David
@yourservice
David_246
Trusted Contributor

Re: Perl mongers

Hi Merijn,

For some reason I didn't get you're update, although I knew you would be able to help me out.
Thanks a lot, this indeed should solve the problem as wel plus that you help me finding the tricky split bracket solution. I've used it manyu times before, I just couldn't remember anymore how the syntax was.

Regs David
@yourservice