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
 
J

jl_post

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,

The very first field? I think you mean the very first line.

I'm a little unclear how anything would print, as your line:

#CPU=<CPU>;

is treated as a comment and therefore will not do anything. I'm going
to guess that you actually wrote:

$CPU=<CPU>;

but just mis-typed your code here.

If that's correct, then you have to know that "$var=<FILEHANDLE>"
only reads ONE LINE from the file into $var. That's why you only see
the first line when you print out $CPU.

Incidentally, there's a much easier way to write:

open(CPU, "cat /proc/cpuinfo|");

Just write it as:

open(CPU, "<", "/proc/cpuinfo");

This way immediately tells a maintainer that the file /proc/cpuinfo is
to be opened for input. (This is also more portable, as not all
platforms may have "cat".)

If you want to read all of the lines you can read all of the lines
in an array, like this:

my @cpuArray = <CPU>;
print @cpuArray; # print out all the lines

or you can activate "slurp" mode (type "perldoc perlvar" and search for
the word "slurp" to see how to do this).
and if i use

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

it will capture all the outputs.

The system() call won't capture any output. It simply runs the "cat
/proc/cpuinfo" command (which leads you to believe that the "print
$cpuinfo;" line of code is printing it out) and assigns $cpuinfo a
value of zero. That's why you probably see that 0 at the end of your
"output."
Can anyone tell me what should i do to filter the only field i want
from the output please. thanks guys

Say you want the field "vendor_id". Just do this:

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

open(IN, "<", "/proc/cpuinfo") or die "Cannot read file: $!";
while (<IN>)
{
# Print the line if it has "vendor_id" in it:
print if m/vendor_id/;
}
close(IN);

__END__

I hope this helps.

-- Jean-Luc
 
B

Bart Lateur

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;

I think you mean

$CPU = <CPU>;

Otherwise it won't do much.
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.

No it won't. system calls the script, letting its STDOUT pass to its own
STDOUT, and thus bypassing your variable. You'd have to use backticks
instead (see perlop)

$cpuinfo = `cat /proc/cpuinfo`;

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

Capture it in an array.

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

@CPU is an array that hold 1 element per line, which probably is what
you want.

You can do the same with backticks:

@CPU = `cat /proc/cpuinfo`;


And, by (temporarily) setting $/ to undef, you can read the whole output
into a scalar with the former syntax, too:

{
local $/; # undef
open(CPU, "cat /proc/cpuinfo|");
$CPU = <CPU>; # all lines
close(CPU);
}

The block is to limit the scope of local.
 
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

Another example of why UNIX should be on the enthusiast category
shelf.
Imagine "cat" as a way to "type/print" a file. Seem's open
source has let too many assholes in.......

Robic0
 
C

Chris Johnson

robic0 said:
Another example of why UNIX should be on the enthusiast category
shelf.
Imagine "cat" as a way to "type/print" a file. Seem's open
source has let too many assholes in.......

Just imagine if it were accessible!

Anyway, I don't know if "asshole" properly describes this poster.
Certainly clueless. I'm wondering how s?he ran into Usenet before a
Camel book.
 
R

robic0

Just imagine if it were accessible!

Anyway, I don't know if "asshole" properly describes this poster.
Certainly clueless. I'm wondering how s?he ran into Usenet before a
Camel book.

I have UNIX-7 source I can cut & paste. Just give me the word and I'll
paste it on the next reply......
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top