how to capture certain fields of the output???

I

infinity12star

Hi people i'm a beginner of Perl language, i'm trying to get a System
Report of any computer, where i can capture the CPU information,
Memory, etc

but i'm having difficulty to capture the right output.
here is my code

open(CPU, "cat /proc/cpuinfo|");
#CPU = <CPU>;
close(CPU);
print $CPU;

with these codes it only captures the very First field of the output
from the CPU info, and if i use

$cpuinfo = system("cat /proc/cpuinfo");
print $cpuinfo;

it will capture all the outputs.

Can anyone tell me what should i do to filter the only field i want
from the output please. thanks guys
 
U

usenet

Hi people i'm a beginner of Perl language

Welcome to Perl. Of particular interest to you may be CPAN, where you
can find thousands of modules to greatly simplify your tasks, as well
as making them much more robust.

Consider this, which uses the Linux::Cupinfo module:

#!/usr/bin/perl
use strict; use warnings;

use Linux::Cpuinfo;
local $\ = "\n";

my $cpu = Linux::Cpuinfo->new();

die ("Could not find cpu info (does /proc/cpuinfo exists?)\n")
unless ref $cpu;

print $cpu -> model_name();
print $cpu -> cpu_mhz();
print $cpu -> cache_size();

__END__
 
C

Chris Johnson

Hi people i'm a beginner of Perl language, i'm trying to get a System
Report of any computer, where i can capture the CPU information,
Memory, etc

but i'm having difficulty to capture the right output.
here is my code

While I suggest in this instance you use the module recommended
previously, you've made several mistakes.
open(CPU, "cat /proc/cpuinfo|");

You can open files directly. If cat can read, perl can too.

open CPU, "/proc/cpuinfo";
#CPU = <CPU>;

I assume that was supposed to be $CPU. Instead use @CPU, as it will
store each line as an element in the array @CPU. (I would also
recommend lowercase variables, but that's not an error.)
close(CPU);
print $CPU;

print @CPU; # assuming you took my previous suggestion.
with these codes it only captures the very First field of the output
from the CPU info, and if i use

$cpuinfo = system("cat /proc/cpuinfo");
print $cpuinfo;

Again, system calls here are unnecessary. Perl's IO is perfectly
capable.
it will capture all the outputs.

Can anyone tell me what should i do to filter the only field i want
from the output please. thanks guys

I recommend you read up on regular expressions and the grep function.
It's far too involved, and it's been done before. If your local or
university library has Programming Perl or Learning Perl, I suggest you
get your hands on those.

Also, if you're a new perl programmer, and there's something you want
to do, chances are fairly good there's a CPAN module that does it
already and comes with a manpage. Go to search.cpan.org.
 
R

robic0

Hi people i'm a beginner of Perl language, i'm trying to get a System
Report of any computer, where i can capture the CPU information,
Memory, etc

but i'm having difficulty to capture the right output.
here is my code

open(CPU, "cat /proc/cpuinfo|");
#CPU = <CPU>;
close(CPU);
print $CPU;

with these codes it only captures the very First field of the output
from the CPU info, and if i use

$cpuinfo = system("cat /proc/cpuinfo");
print $cpuinfo;

it will capture all the outputs.

Can anyone tell me what should i do to filter the only field i want
from the output please. thanks guys

Most puzzleing why you would incorporate a, I guess
STDIO, pipe mechanism into an open.
It just might be the eol is the eof in that case.

If you trust the (print) cpuinfo, why not just "system" it
to a file then read the file in?

Don't know unix per say..
 
C

Chris Johnson

robic0 said:
Most puzzleing why you would incorporate a, I guess
STDIO, pipe mechanism into an open.
It just might be the eol is the eof in that case.

If you trust the (print) cpuinfo, why not just "system" it
to a file then read the file in?

Don't know unix per say..

In Unix, everything's a file, so there's no need to do any redirection.
This user apparently just doesn't know how to read a file directly.
 
R

robic0

While I suggest in this instance you use the module recommended
previously, you've made several mistakes.


You can open files directly. If cat can read, perl can too.

open CPU, "/proc/cpuinfo";


I assume that was supposed to be $CPU. Instead use @CPU, as it will
store each line as an element in the array @CPU. (I would also
recommend lowercase variables, but that's not an error.)


print @CPU; # assuming you took my previous suggestion.


Again, system calls here are unnecessary. Perl's IO is perfectly
capable.


I recommend you read up on regular expressions and the grep function.
It's far too involved, and it's been done before. If your local or
university library has Programming Perl or Learning Perl, I suggest you
get your hands on those.

Also, if you're a new perl programmer, and there's something you want
to do, chances are fairly good there's a CPAN module that does it
already and comes with a manpage. Go to search.cpan.org.

For this simple example, if you would like to post an absolute working
set, that would be appretiated.

OP be warned, CPAN is not 'Quality Assured'. I've used modules,
most of them are flawwed. If someone wants to make an argument that
there are no CPAN module flaws, feel free to post QA tests right here
below this line:
----------------------------------------

----------------------------------------
Otherwise in my experince CPAN modules are each loaded with a whole
lotta bugs and problems. Of the CPAN modules I've used, I have a
%100 failure usage. Meaning that it didn't work as proclaimed.
One clue is the Versioning by the authors. And the lack of real
changes with versions typically done by 'C' authors.

Could it be these module writers encapsulate other modules that
are intrinsically flawwed? You bet your ass! The biggest flawwed
module is Perl itself!

Robic0
 
R

robic0

In Unix, everything's a file, so there's no need to do any redirection.
This user apparently just doesn't know how to read a file directly.

Ooops, ok sounds fair. You might not like my other follow on your response.
But fair enough!!
 
C

Chris Johnson

robic0 said:
For this simple example, if you would like to post an absolute working
set, that would be appretiated.

Personally, I would use the CPAN module. But if I were to do it myself,
it would look something like:

#!/usr/bin/perl
use strict;

open CPUINFO, "/proc/cpuinfo";
my @cpuinfo = <CPUINFO>;
close CPUINFO;

print grep {/vendor_id/} @cpuinfo;
 
D

Dr.Ruud

Chris Johnson schreef:
This user apparently just doesn't know how to read a
file directly.

Or maybe his cat-executable has more rights than his user-account...
;)
 
J

Josef Möllers

Dr.Ruud said:
Chris Johnson schreef:


Or maybe his cat-executable has more rights than his user-account...
;)

At least on my box (SuSE 9.2), /proc/cpuinfo is world-readable.
There's nothing secret there.
 
D

Dr.Ruud

Josef Möllers:
Dr.Ruud:

At least on my box (SuSE 9.2), /proc/cpuinfo is world-readable.
There's nothing secret there.

With an omnipotent cat, why would /proc/cpuinfo be his final source?
 
I

infinity12star

thx it works, but another problem

if i grep {/model name/} from the @cpuinfo, the output i get is

model name : Intel(R) Pentium(R) xxxxxx etc
but i jst want the output after :
which is Intel(R) .....
must i use Split or wht should i do with it?
 
A

Anno Siegel

thx it works, but another problem

if i grep {/model name/} from the @cpuinfo, the output i get is

model name : Intel(R) Pentium(R) xxxxxx etc
but i jst want the output after :
which is Intel(R) .....
must i use Split or wht should i do with it?

That's a possibility. Try it.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top