how to capture certain fields of the output

Discussion in 'Perl Misc' started by infinity12star@gmail.com, Mar 21, 2006.

  1. Guest

    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
    , Mar 21, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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
    , Mar 21, 2006
    #2
    1. Advertising

  3. Bart Lateur Guest

    wrote:

    >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.

    --
    Bart.
    Bart Lateur, Mar 21, 2006
    #3
  4. robic0 Guest

    On 21 Mar 2006 12:07:40 -0800, wrote:

    >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
    robic0, Mar 22, 2006
    #4
  5. robic0 wrote:
    > On 21 Mar 2006 12:07:40 -0800, wrote:
    >
    > >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.......


    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.
    Chris Johnson, Mar 22, 2006
    #5
  6. robic0 Guest

    On 21 Mar 2006 16:59:58 -0800, "Chris Johnson" <> wrote:

    >
    >robic0 wrote:
    >> On 21 Mar 2006 12:07:40 -0800, wrote:
    >>
    >> >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.......

    >
    >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......
    robic0, Mar 22, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    7
    Views:
    9,080
  2. Replies:
    0
    Views:
    637
  3. Replies:
    2
    Views:
    505
    bruce barker
    Mar 25, 2008
  4. SAN CAZIANO
    Replies:
    8
    Views:
    165
    Dr John Stockton
    Oct 15, 2004
  5. Replies:
    12
    Views:
    157
    Anno Siegel
    Mar 22, 2006
Loading...

Share This Page