Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: Display the specific line in a file

Reply
Thread Tools

Help: Display the specific line in a file

 
 
Amy Lee
Guest
Posts: n/a
 
      02-13-2008
Hello,

I'm a sysadmin, I wanna write a small script to show information about
/proc directory. For example, I wanna show the cpuinfo file and just two
items "vendor_id" and "model name".

Here's the file (part):
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)
stepping : 10
cpu MHz : 1000.045
...........

I suppose that I should use grep command to do that. However, I don't know
how to accomplish it.

Could you give me some tips?

Thank you very much.

Amy Lee
 
Reply With Quote
 
 
 
 
Achim Peters
Guest
Posts: n/a
 
      02-13-2008
Amy Lee schrieb:
> I'm a sysadmin, I wanna write a small script to show information about
> /proc directory. For example, I wanna show the cpuinfo file and just two
> items "vendor_id" and "model name".
>
> Here's the file (part):
> processor : 0
> vendor_id : GenuineIntel
> cpu family : 6
> model : 8
> model name : Pentium III (Coppermine)
> stepping : 10
> cpu MHz : 1000.045
> ..........
>
> I suppose that I should use grep command to do that. However, I don't know
> how to accomplish it.


What's wrong with

#!/bin/sh
grep "^vendor_id" /proc/cpuinfo
grep "^model name" /proc/cpuinfo

except that it's not perl? For a "script" that has to "show the cpuinfo
file and just two items" you would not need perl, I presume.

If it has to be perl, the following might work for you

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

open CPUINFO, "<", "/proc/cpuinfo"
or die "Can't open /proc/cpuinfo. $!";

while (<CPUINFO>) {
if ( /^vendor_id/ or /^model name/) {
print;
}
}

Bye
Achim
 
Reply With Quote
 
 
 
 
Amy Lee
Guest
Posts: n/a
 
      02-13-2008
On Wed, 13 Feb 2008 16:06:29 +0100, Achim Peters wrote:

> Amy Lee schrieb:
>> I'm a sysadmin, I wanna write a small script to show information about
>> /proc directory. For example, I wanna show the cpuinfo file and just two
>> items "vendor_id" and "model name".
>>
>> Here's the file (part):
>> processor : 0
>> vendor_id : GenuineIntel
>> cpu family : 6
>> model : 8
>> model name : Pentium III (Coppermine)
>> stepping : 10
>> cpu MHz : 1000.045
>> ..........
>>
>> I suppose that I should use grep command to do that. However, I don't know
>> how to accomplish it.

>
> What's wrong with
>
> #!/bin/sh
> grep "^vendor_id" /proc/cpuinfo
> grep "^model name" /proc/cpuinfo
>
> except that it's not perl? For a "script" that has to "show the cpuinfo
> file and just two items" you would not need perl, I presume.
>
> If it has to be perl, the following might work for you
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> open CPUINFO, "<", "/proc/cpuinfo"
> or die "Can't open /proc/cpuinfo. $!";
>
> while (<CPUINFO>) {
> if ( /^vendor_id/ or /^model name/) {
> print;
> }
> }
>
> Bye
> Achim

Thanks very much. Anyway, if I wanna change the output, just like replace
vendor_id to VENDOR, how can I do in your perl codes?

Thanks again.

Amy Lee
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-13-2008
Amy Lee wrote:
> On Wed, 13 Feb 2008 16:06:29 +0100, Achim Peters wrote:
>
>> Amy Lee schrieb:
>>> I'm a sysadmin, I wanna write a small script to show information about
>>> /proc directory. For example, I wanna show the cpuinfo file and just two
>>> items "vendor_id" and "model name".
>>>
>>> Here's the file (part):
>>> processor : 0
>>> vendor_id : GenuineIntel
>>> cpu family : 6
>>> model : 8
>>> model name : Pentium III (Coppermine)
>>> stepping : 10
>>> cpu MHz : 1000.045
>>> ..........
>>>
>>> I suppose that I should use grep command to do that. However, I don't know
>>> how to accomplish it.

>> What's wrong with
>>
>> #!/bin/sh
>> grep "^vendor_id" /proc/cpuinfo
>> grep "^model name" /proc/cpuinfo
>>
>> except that it's not perl? For a "script" that has to "show the cpuinfo
>> file and just two items" you would not need perl, I presume.
>>
>> If it has to be perl, the following might work for you
>>
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>> open CPUINFO, "<", "/proc/cpuinfo"
>> or die "Can't open /proc/cpuinfo. $!";
>>
>> while (<CPUINFO>) {
>> if ( /^vendor_id/ or /^model name/) {
>> print;
>> }
>> }

>
> Thanks very much. Anyway, if I wanna change the output, just like replace
> vendor_id to VENDOR, how can I do in your perl codes?


while (<CPUINFO>) {
if ( s/^vendor_id/VENDOR/ or /^model name/ ) {
print;
}
}


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Achim Peters
Guest
Posts: n/a
 
      02-13-2008
Amy Lee schrieb:
> On Wed, 13 Feb 2008 16:06:29 +0100, Achim Peters wrote:
>
>> Amy Lee schrieb:
>>> I'm a sysadmin, I wanna write a small script to show information about
>>> /proc directory.


>> If it has to be perl, the following might work for you


> Thanks very much. Anyway, if I wanna change the output, just like replace
> vendor_id to VENDOR, how can I do in your perl codes?


How about

$good_book = "Learning Perl";
$preferred_beverage = "Cup o'Tea";
# don't wanna recommend Cup o'Java here ...

open BOOK "<" $good_book or die $!;
while (<BOOK>) {
read;
sip($preferred_beverage);
}

first?

Bye
Achim
 
Reply With Quote
 
Amy Lee
Guest
Posts: n/a
 
      02-13-2008
On Wed, 13 Feb 2008 15:29:01 +0000, John W. Krahn wrote:

> Amy Lee wrote:
>> On Wed, 13 Feb 2008 16:06:29 +0100, Achim Peters wrote:
>>
>>> Amy Lee schrieb:
>>>> I'm a sysadmin, I wanna write a small script to show information about
>>>> /proc directory. For example, I wanna show the cpuinfo file and just two
>>>> items "vendor_id" and "model name".
>>>>
>>>> Here's the file (part):
>>>> processor : 0
>>>> vendor_id : GenuineIntel
>>>> cpu family : 6
>>>> model : 8
>>>> model name : Pentium III (Coppermine)
>>>> stepping : 10
>>>> cpu MHz : 1000.045
>>>> ..........
>>>>
>>>> I suppose that I should use grep command to do that. However, I don't know
>>>> how to accomplish it.
>>> What's wrong with
>>>
>>> #!/bin/sh
>>> grep "^vendor_id" /proc/cpuinfo
>>> grep "^model name" /proc/cpuinfo
>>>
>>> except that it's not perl? For a "script" that has to "show the cpuinfo
>>> file and just two items" you would not need perl, I presume.
>>>
>>> If it has to be perl, the following might work for you
>>>
>>> #!/usr/bin/perl
>>> use strict;
>>> use warnings;
>>>
>>> open CPUINFO, "<", "/proc/cpuinfo"
>>> or die "Can't open /proc/cpuinfo. $!";
>>>
>>> while (<CPUINFO>) {
>>> if ( /^vendor_id/ or /^model name/) {
>>> print;
>>> }
>>> }

>>
>> Thanks very much. Anyway, if I wanna change the output, just like replace
>> vendor_id to VENDOR, how can I do in your perl codes?

>
> while (<CPUINFO>) {
> if ( s/^vendor_id/VENDOR/ or /^model name/ ) {
> print;
> }
> }
>
>
> John

Thank you very much!
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-13-2008

Quoth Achim Peters <(E-Mail Removed)>:
>
> How about


Where is

use strict;
use warnings;

> $good_book = "Learning Perl";


my $good_book = ...;

> $preferred_beverage = "Cup o'Tea";
> # don't wanna recommend Cup o'Java here ...
>
> open BOOK "<" $good_book or die $!;


You're missing a comma after "<".
You should use lexical filehandles.
You should say what you were trying to open.

open my $BOOK, '<', $good_book
or die "can't open '$good_book': $!";

> while (<BOOK>) {


Would you really read a book line-by-line? I would expect to leave at
least a paragraph between sips, so we need

local $/ = '';

before the while loop.

> read;


Not enough arguments for read...

You probably want to choose a different sub name.

> sip($preferred_beverage);
> }




Ben

 
Reply With Quote
 
Achim Peters
Guest
Posts: n/a
 
      02-13-2008
Ben Morrow schrieb:
> Quoth Achim Peters <(E-Mail Removed)>:
>> How about

>
> Where is
>
> use strict;
> use warnings;


Who said it was supposed to be perl?

>


~

Bye
Achim
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      02-14-2008
Amy Lee <(E-Mail Removed)> wrote:

> I wanna show the cpuinfo file and just two
> items "vendor_id" and "model name".



> Could you give me some tips?



perl -ne 'print if /^(vendor_id|model name)/' /proc/cpuinfo


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Is ViwState Page-Specific or UserControl-Specific =?Utf-8?B?SmF2?= ASP .Net 2 08-16-2006 09:30 PM
redirect traffic on specific ip to specific interface mimiseh Cisco 3 06-05-2005 09:14 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments