Home: Perl Programming Help: Beginner:
Help from an Expert on simple code

New User

Sep 23, 2005, 9:28 PM

Views: 1438
Help from an Expert on simple code


I'm looking for some guidance/assistance with a code I'm trying to come
up with. Machine: Redhat 9

I'm looking for a simple code that will execute something like this:
cat /proc/cpuinfo |grep processor

cat /proc/cpuinfo |grep MHz

execute lspci |grep 00:07.0
but if it encounters more then 1 line for 00:07.0 ignore it, and only print once.

Then somehow print it to display like this:
processor : 0 cpu MHz : 497.443
00:07.0 ISA bridge: Intel Corp. 82371AB/EB/MB PIIX4 ISA (rev 02)

Can someone give me a hand on this?

Any help would be greatly appreciated.


New User

Sep 24, 2005, 7:36 PM

Views: 1434
Re: [jay] Help from an Expert on simple code

I'm just looking for a little help from the experts. Just
a couple of lines or the sintex to exec the commands within perl i.e.:

#!/usr/bin/perl -w

@output1 = exec cat /proc/cpuinfo |grep processor;
print @output1 /n;

any guidance? I'm not sure how to exec commands here, that's why I'm posting this - for help. thx

New User

Sep 25, 2005, 10:24 PM

Views: 1431
Re: [jay] Help from an Expert on simple code

If you use the exec command, your perl script executes the supplied command and exits itself so you will not cache the output to @output1, try backticks instead:

use warnings;
use strict;
my @output1 = `cat /proc/cpuinfo | grep processor`;
print "@output1\n";

Of course instead of using the system grep you can use perl buildin grep, map or regexps.

Thaumaturge / Moderator

Sep 26, 2005, 2:19 AM

Views: 1428
Re: [jay] Help from an Expert on simple code

You use the qx() operator to execute an external command an capture the output.

my @cpuinfo = qw(grep processor /proc/cpuinfo);

You'll often see that written using backticks instead - that's just another version of the same thing.

my @cpuinfo = `grep processor /proc/cpuinfo`;

p.s. This really isn't "advanced", so I've moved the thread into the beginners' forum and removed the copy that you had already put there. Please don't ask the same question in multiple forums.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks

(This post was edited by davorg on Sep 26, 2005, 3:24 AM)

New User

Sep 26, 2005, 3:38 PM

Views: 1421
Re: [davorg] Help from an Expert on simple code

Thanks. This is what I was looking for. Thx fellows.