Help: Display the specific line in a file

Discussion in 'Perl Misc' started by Amy Lee, Feb 13, 2008.

  1. Amy Lee

    Amy Lee Guest

    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
     
    Amy Lee, Feb 13, 2008
    #1
    1. Advertising

  2. Amy Lee

    Achim Peters Guest

    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
     
    Achim Peters, Feb 13, 2008
    #2
    1. Advertising

  3. Amy Lee

    Amy Lee Guest

    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
     
    Amy Lee, Feb 13, 2008
    #3
  4. 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
     
    John W. Krahn, Feb 13, 2008
    #4
  5. Amy Lee

    Achim Peters Guest

    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
     
    Achim Peters, Feb 13, 2008
    #5
  6. Amy Lee

    Amy Lee Guest

    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!
     
    Amy Lee, Feb 13, 2008
    #6
  7. Amy Lee

    Ben Morrow Guest

    Quoth Achim Peters <>:
    >
    > 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
     
    Ben Morrow, Feb 13, 2008
    #7
  8. Amy Lee

    Achim Peters Guest

    Ben Morrow schrieb:
    > Quoth Achim Peters <>:
    >> How about

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


    Who said it was supposed to be perl?

    > :)


    :)~

    Bye
    Achim
     
    Achim Peters, Feb 13, 2008
    #8
  9. Amy Lee <> 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/"
     
    Tad J McClellan, Feb 14, 2008
    #9
    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. Hugo
    Replies:
    10
    Views:
    1,325
    Matt Humphrey
    Oct 18, 2004
  2. =?Utf-8?B?SmF2?=

    Is ViwState Page-Specific or UserControl-Specific

    =?Utf-8?B?SmF2?=, Aug 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    548
    =?Utf-8?B?SmF2?=
    Aug 16, 2006
  3. kaushikshome
    Replies:
    4
    Views:
    779
    kaushikshome
    Sep 10, 2006
  4. scad
    Replies:
    23
    Views:
    1,178
    Alf P. Steinbach
    May 17, 2009
  5. mazdotnet
    Replies:
    2
    Views:
    406
    Alexey Smirnov
    Oct 2, 2009
Loading...

Share This Page