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: Removing text

2015-05-18 Thread Richard Taubo
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. :-)

Best regards,
Richard Taubo




> On 18 May 2015, at 17:52, Shawn H Corey  wrote:
> 
> On Mon, 18 May 2015 17:32:03 +0200
> Richard Taubo  wrote:
> 
>> Hi!
>> 
>> Trying to remove the literal text "%st" from the command line return
>> value: 0.0%st as in:
>> [$] printf "0.0%st" | perl -pe 's/\%st//'
>> 
>> I have also tried: 
>> 
>> [$] printf "0.0%st" | perl -pe 's/\Q%st\E//'
>> 
>> Neither works.
>> 
>> Would be happy if someone had any input here! :-)
>> 
>> Thanks!
>> 
>> Richard Taubo
> 
> I'm not sure what you're trying to do but if you want a percent sign in
> a printf specification, use two of them.
> 
>printf "Sale: %d%% off", $percentage_off;
> 
> See `perldoc -f sprintf`
> 

And:

> On 18 May 2015, at 17:49, Jing Yu  wrote:
> 
> Hi Richard,
> 
> When you 
>printf "0.0%st”
> in the command line, it prints
>0.0t
> And that is the string piped to perl. This is perhaps why you didn’t succeed.


--
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-18 Thread Shawn H Corey
On Mon, 18 May 2015 17:32:03 +0200
Richard Taubo  wrote:

> Hi!
> 
> Trying to remove the literal text "%st" from the command line return
> value: 0.0%st as in:
> [$] printf "0.0%st" | perl -pe 's/\%st//'
> 
> I have also tried: 
> 
> [$] printf "0.0%st" | perl -pe 's/\Q%st\E//'
> 
> Neither works.
> 
> Would be happy if someone had any input here! :-)
> 
> Thanks!
> 
> Richard Taubo

I'm not sure what you're trying to do but if you want a percent sign in
a printf specification, use two of them.

printf "Sale: %d%% off", $percentage_off;

See `perldoc -f sprintf`



-- 
Don't stop where the ink does.
Shawn

-- 
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-18 Thread Jing Yu
Hi Richard,

When you 
printf "0.0%st”
in the command line, it prints
0.0t
And that is the string piped to perl. This is perhaps why you didn’t succeed.

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




Removing text

2015-05-18 Thread Richard Taubo
Hi!

Trying to remove the literal text "%st" from the command line return value: 
0.0%st
as in:
[$] printf "0.0%st" | perl -pe 's/\%st//'

I have also tried: 

[$] printf "0.0%st" | perl -pe 's/\Q%st\E//'

Neither works.

Would be happy if someone had any input here! :-)

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: Removing text to the end of the line.

2002-12-19 Thread John W. Krahn
David Buddrige wrote:
> 
> Hi all,

Hello,

> I have a bunch of C++ source files that contain the string
> 
> PCN: sometext
> 
> where somtext is an arbitary sequence of characters.
> 
> Whenever the text "PCN:" occurs, I want to remove it and everything else
> to the end of the line.
> 
> To do this, I have written the following script:
> 
> while(<>)
> {
>  my $current_line;
> 
>  $current_line = $_;
>  $current_line =~ s/PCN:[.]*//g;
> 
>  print $current_line;
> }
> 
> However this only removes the "PCN:" part, leaving the rest of the
> string that occurs between the PCN: and the new-line undisturbed.  Is
> there some way that I can tell perl that I want it to remove everything
> from PCN: onwards to the end of the line?

Putting the period in a character class [.] means to match a literal
period character.  You want to use the dot metacharacter that matches
any character except newline.

while ( my $current_line = <> )
{
 $current_line =~ s/PCN:.*//;

 print $current_line;
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Removing text to the end of the line.

2002-12-19 Thread David Buddrige
Hi all,

Once again - I figured it out... here is the updated script:

while(<>)
{
my $current_line;

$current_line = $_;
$current_line =~ s/PCN:.*//g;

print $current_line;
}

It worked after I took out the square brackets - although I am not sure 
why - since the square brackets *should* be benign if not actually 
needed.  It should just mean any of the characters therein - or is it 
seeing the . as a . rather than as a meta-character meaning any-character?

cheers

David Buddrige.



David Buddrige wrote:
Hi all,

I have a bunch of C++ source files that contain the string

PCN: sometext

where somtext is an arbitary sequence of characters.

Whenever the text "PCN:" occurs, I want to remove it and everything else 
to the end of the line.

To do this, I have written the following script:

while(<>)
{
my $current_line;

$current_line = $_;
$current_line =~ s/PCN:[.]*//g;

print $current_line;
}


However this only removes the "PCN:" part, leaving the rest of the 
string that occurs between the PCN: and the new-line undisturbed.  Is 
there some way that I can tell perl that I want it to remove everything 
from PCN: onwards to the end of the line?

thanks heaps guys

David Buddrige.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Removing text to the end of the line.

2002-12-19 Thread David Buddrige
Hi all,

I have a bunch of C++ source files that contain the string

PCN: sometext

where somtext is an arbitary sequence of characters.

Whenever the text "PCN:" occurs, I want to remove it and everything else 
to the end of the line.

To do this, I have written the following script:

while(<>)
{
my $current_line;

$current_line = $_;
$current_line =~ s/PCN:[.]*//g;

print $current_line;
}


However this only removes the "PCN:" part, leaving the rest of the 
string that occurs between the PCN: and the new-line undisturbed.  Is 
there some way that I can tell perl that I want it to remove everything 
from PCN: onwards to the end of the line?

thanks heaps guys

David Buddrige.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]