Re: Removing text

2015-05-19 Thread Brandon McCaig
On Tue, May 19, 2015 at 1:10 PM, Brandon McCaig  wrote:
> 1234567890Richard:

Sorry, that should have been just "Richard:". I will blame my flaky
browser-based user interface. I should have switched to mutt to write
that.

Regards,


-- 
Brandon McCaig  
Castopulence Software 
Blog 
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Removing text

2015-05-19 Thread Brandon McCaig
1234567890Richard:

On Mon, May 18, 2015 at 12:27 PM, Richard Taubo  wrote:
> Hi,

Hello,

> The full (bash) script with perl parts looks like this:
> [$] top_return=$(top -n1 | head -5)
> [$] io_return=$(printf "%s\n" "$top_return" | grep "^Cpu(s)")
> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
> 's/\%st//')
> [$] printf "%s\n" "$io_all"

Since you're using Perl here I wonder why you're bothering with bash
and shell commands. Since the output of those commands will vary by
system (I can't be sure what you're trying based on my results) it's
impossible for us to debug your problem. However, we can demonstrate a
more straightforward, Perl-only solution using output from our own
system.

use autodie;
use strict;
use warnings;

use Data::Dumper;

$Data::Dumper::Useqq = 1;

main();
exit(0);

sub main {
# On my system top outputs control characters for colors and such.
# This is a simple way to convince it to do nothing special.
my $term = $ENV{TERM};

$ENV{TERM} = 'dumb';

my $cpu_stats = parse_cpu_stats();

$ENV{TERM} = $term;

print Dumper $cpu_stats;
}

sub parse_cpu_stats {
# Use a pipe to top(1) to read the output directly.
open my $fh, '-|', qw/top -n1/;

while (my $line = <$fh>) {
chomp($line);

# Look for the Cpu line.
if ($line =~ /Cpu/) {
return parse_pairs($line);
}
}
}

sub parse_pairs {
my ($line) = @_;

# Results in (0.5, 'us', 0.1, 'sy', 0.0, 'ni', 99.1, 'id',
# 0.3, 'wa', 0.0, 'hi', 0.0, 'si', 0.0, 'st');
my @pairs = $line =~ /([0-9]+\.[0-9]+)\s+(..)/gi;

# Results in (us => 0.5, sy => 0.1 ni => 99.1 id => 0.3,
# hi => 0.0, si => 0.0, st => 0.0)
my %fields = reverse @pairs;

return \%fields;
}

__END__

This was hacked against the Debian Linux top(1) implementation.
Example output is:

> %Cpu(s):  0.5 us,  0.1 sy,  0.0 ni, 99.1 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 
> st

Output from the above program is:

$VAR1 = {
  "wa" => "0.3",
  "hi" => "0.0",
  "ni" => "0.0",
  "st" => "0.0",
  "us" => "0.5",
  "id" => "99.1",
  "si" => "0.0",
  "sy" => "0.1"
};

I'm not saying this is a better or more robust solution than hacking
it up in bash, but at least by using Perl you have the full power of
Perl without switching between bash, grep, awk, and perl.

The Interwebz suggests that checking /proc would be a better solution,
though that would vary by the *nix system. In general, you should
prefer CPAN modules for this since the solution is flaky, and
hopefully somebody else has already done any heavy lifting and ugly
work for you... I didn't immediately see a module that looked like a
drop-in replacement, but I would be surprised if nothing exists yet.

Regards,


-- 
Brandon McCaig  
Castopulence Software 
Blog 
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re[2]: Removing text

2015-05-19 Thread Артём Варнайский



Вторник, 19 мая 2015, 11:02 +02:00 от Richard Taubo :
>Hi!
>
>
>> On 18 May 2015, at 18:27, Richard Taubo < o...@bergersen.no > wrote:
>> 
>> Hi, 
>> 
>> and thanks to Shawn H Corey, Jing Yu.
>> 
>> I see that I have not been specific enough.
>> (And sorry for the top posting).
>> 
>> The full (bash) script with perl parts looks like this:
>> [$] top_return=$(top -n1 | head -5)
>> [$] io_return=$(printf "%s\n" "$top_return" | grep "^Cpu(s)")
>> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
>> 's/\%st//')
>> [$] printf "%s\n" "$io_all"
>> 
>> Returns =>  0.0%st   (would want it to be: 0.0 without the '%st' 
>> part).
>> 
>> This "kind of works":
>> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
>> 's/\%//')
>> [$] printf "%s\n" "$io_all"
>> Returns =>  0.0st (without the % sign)
>> 
>> This too:
>> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
>> 's/st//')
>> [$] printf "%s\n" "$io_all"
>> Returns =>  0.0% (without the "st" characters)
>> 
>> BUT when i search and replace for "%st" (as described above), I can’t
>> get it to work.
>> 
>> Thanks for any feedback, and thanks again for the answers I have received. 
>> :-)
>
>In the end, I ended up with two separate perl commands.
>One removing the "%" part of the text, the other the "st" part of the text:
Mistake. Here comes fix:
io_all=$(printf "%s\n" "$cp_return" | awk '{ printf $9 }'  | perl -pe 
's/.*/$&st/;'


Re[2]: Removing text

2015-05-19 Thread Артём Варнайский



Вторник, 19 мая 2015, 11:02 +02:00 от Richard Taubo :
>Hi!
>
>
>> On 18 May 2015, at 18:27, Richard Taubo < o...@bergersen.no > wrote:
>> 
>> Hi, 
>> 
>> and thanks to Shawn H Corey, Jing Yu.
>> 
>> I see that I have not been specific enough.
>> (And sorry for the top posting).
>> 
>> The full (bash) script with perl parts looks like this:
>> [$] top_return=$(top -n1 | head -5)
>> [$] io_return=$(printf "%s\n" "$top_return" | grep "^Cpu(s)")
>> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
>> 's/\%st//')
>> [$] printf "%s\n" "$io_all"
>> 
>> Returns =>  0.0%st   (would want it to be: 0.0 without the '%st' 
>> part).
>> 
>> This "kind of works":
>> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
>> 's/\%//')
>> [$] printf "%s\n" "$io_all"
>> Returns =>  0.0st (without the % sign)
>> 
>> This too:
>> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
>> 's/st//')
>> [$] printf "%s\n" "$io_all"
>> Returns =>  0.0% (without the "st" characters)
>> 
>> BUT when i search and replace for "%st" (as described above), I can’t
>> get it to work.
>> 
>> Thanks for any feedback, and thanks again for the answers I have received. 
>> :-)
>
>In the end, I ended up with two separate perl commands.
>One removing the "%" part of the text, the other the "st" part of the text:
>
>io_all=$(printf "%s\n" "$cp_return" | awk '{ printf $9 }' | perl -pe 's/%//' | 
>perl -pe 's/st//')
>Returns =>  0.0 (without the "%st" characters)
>
>That works fine, but looks a little clunky, are there perhaps other ways to 
>unite the two
>perl commands?
>
>Thanks!
>
>Richard Taubo
>--
>To unsubscribe, e-mail:  beginners-unsubscr...@perl.org
>For additional commands, e-mail:  beginners-h...@perl.org
>http://learn.perl.org/
>
>
Hello. Try this:

io_all=$(printf "%s\n" "$cp_return" | awk '{ printf $9 }' | perl -pe 's/%//' | 
perl -pe 's/.*/$&st/;'




Re: Removing text

2015-05-19 Thread Richard Taubo
Hi!


> On 18 May 2015, at 18:27, Richard Taubo  wrote:
> 
> Hi, 
> 
> and thanks to Shawn H Corey, Jing Yu.
> 
> I see that I have not been specific enough.
> (And sorry for the top posting).
> 
> The full (bash) script with perl parts looks like this:
> [$] top_return=$(top -n1 | head -5)
> [$] io_return=$(printf "%s\n" "$top_return" | grep "^Cpu(s)")
> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
> 's/\%st//')
> [$] printf "%s\n" "$io_all"
> 
> Returns =>  0.0%st(would want it to be: 0.0 without the '%st' 
> part).
> 
> This "kind of works":
> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
> 's/\%//')
> [$] printf "%s\n" "$io_all"
> Returns =>  0.0st (without the % sign)
> 
> This too:
> [$] io_all=$(printf  "%s" "$io_return" | awk '{ printf $9 }' | perl -pe 
> 's/st//')
> [$] printf "%s\n" "$io_all"
> Returns =>  0.0% (without the "st" characters)
> 
> BUT when i search and replace for "%st" (as described above), I can’t
> get it to work.
> 
> Thanks for any feedback, and thanks again for the answers I have received. :-)

In the end, I ended up with two separate perl commands.
One removing the "%" part of the text, the other the "st" part of the text:

io_all=$(printf "%s\n" "$cp_return" | awk '{ printf $9 }' | perl -pe 's/%//' | 
perl -pe 's/st//')
Returns =>  0.0 (without the "%st" characters)

That works fine, but looks a little clunky, are there perhaps other ways to 
unite the two
perl commands?

Thanks!

Richard Taubo
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: yahoo quotes download no longer working

2015-05-19 Thread Mike McClain
 $Bill wrote:
On Sat, May 16, 2015 at 05:57:35PM -0700, Mike McClain wrote:
> The symptom is that http://download.finance.yahoo.com returns 404 Not Found
> and has for a couple of weeks now.
> Mike

This worked for me:
http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab

Result:
"Apple Inc.",128.80,128.78
"Google Inc.",534.00,533.00
"Microsoft Corporation",48.310,48.260


Also, a non-csv quote which is less useful:
http://finance.yahoo.com/q?s=YHOO

--
Where man is there will be trouble to the end of time,
if not of one sort, then of another."
- Louis L'Amour

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/