Help: Display the specific line in a file

A

Amy Lee

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
 
A

Achim Peters

Amy said:
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
 
A

Amy Lee

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
 
J

John W. Krahn

Amy said:
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
 
A

Achim Peters

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
 
B

Ben Morrow

Quoth Achim Peters said:
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.

Not enough arguments for read...

You probably want to choose a different sub name.
sip($preferred_beverage);
}

:)

Ben
 
T

Tad J McClellan

Amy Lee said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top