Newbie needs help on pattern matching

Discussion in 'Perl Misc' started by Madhusudan Singh, Sep 2, 2004.

  1. Hi

    I am kind of new to perl (dabbled in it a little bit 4-5 years ago, but
    never needed it till now - I am writing an application).

    I need to extract some info from /sbin/iwconfig eth1 :

    $ /sbin/iwconfig eth1 | grep "Link"

    Link Quality:90/92 Signal level:-8 dBm Noise level:-148 dBm
    (updated)

    I want to extract 90/92 from the stuff above. How do I code it ?

    If it is possible to use awk to do the above ('{print $2}' yields
    "Quality:90/92", when I need to extract 90/92 or even better 90 and 92
    separately, that is also ok.

    How does one embed an awk command in perl ? I tried :

    $info=`/sbin/iwconfig eth1 | grep "Link" | awk '{print $2}'`; but it did
    not work (just reproduced o/p as if the second pipe were not even present).

    Thanks.
     
    Madhusudan Singh, Sep 2, 2004
    #1
    1. Advertising

  2. Madhusudan Singh <> wrote:

    > I need to extract some info from


    > Link Quality:90/92 Signal level:-8 dBm Noise level:-148 dBm


    > I want to extract 90/92 from the stuff above. How do I code it ?



    with the m// operator and the appropriate regex.


    if ( $_ =~ m/Link Quality:(\S*)/ ) {
    $qual = $1;
    }


    > I need to extract 90/92 or even better 90 and 92
    > separately,



    Use a m// in list context instead of the above then:

    my($first, $second) = $_ =~ m!Link Quality:(\d+)/(\d+)!;


    > How does one embed an awk command in perl ? I tried :

    ^^^^^^^
    ^^^^^^^

    The same way you embed any other external program call in Perl.

    Please spend a few seconds searching the Perl FAQ *before* posting
    to the Perl newsgroup.

    perldoc -q command

    Q: How can I capture STDERR from an external command?

    A: There are three basic ways of running external commands: ...


    Or, write what you need in awk, and then run it through the "a2p"
    (awk to Perl) translator program that ships with the perl distribution.



    But anything you can do with awk, you can do with native Perl, so
    you don't need to shell-out to awk anyways. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 2, 2004
    #2
    1. Advertising

  3. Madhusudan Singh wrote:

    > Subject: Newbie needs help on pattern matching


    Subject lines are important don't waste space with words like "Newbie
    needs help on" that carry no valusable information. The waste of space
    in the subject lines only serves to predispose people against you.

    > I am kind of new to perl (dabbled in it a little bit 4-5 years ago, but
    > never needed it till now - I am writing an application).
    >
    > I need to extract some info from /sbin/iwconfig eth1 :
    >
    > $ /sbin/iwconfig eth1 | grep "Link"
    >
    > Link Quality:90/92 Signal level:-8 dBm Noise level:-148 dBm
    > (updated)
    >
    > I want to extract 90/92 from the stuff above. How do I code it ?


    It is more easily done in a single step.

    my ($link_quality) = `/sbin/iwconfig eth1` =~ /Link Quality:(\S+)/;

    Note: this isn't very smart wrt errors. If the command fails in any way
    to emit the string 'Link Quality:' then $link_quality will simply be
    undefined.
    >
    > If it is possible to use awk to do the above ('{print $2}' yields
    > "Quality:90/92", when I need to extract 90/92 or even better 90 and 92
    > separately, that is also ok.
    >
    > How does one embed an awk command in perl ? I tried :


    Why would you want to? You can usually simply re-state it more simply
    in Perl.

    >
    > $info=`/sbin/iwconfig eth1 | grep "Link" | awk '{print $2}'`; but it did
    > not work (just reproduced o/p as if the second pipe were not even present).


    The `` quoting construct is interpolative - if you want to put a literal
    $ into the command that is passed to /bin/sh you'll need to escape it
    with a backslash. If you don't then the $2 in the command will be
    replaced with the value of the Perl variable $2.

    BTW: Did you perhaps "forget" to enable warnings in Perl?

    Do not attempt to learn to free-climb from the outset. Learn to climb
    with ropes. Then and only then should you consider sometimes going
    without. It may seem like more work - right upto the point where you
    first slip and fall.

    In Perl the ropes are 'strict' and 'warnings'. If you have problems
    because to fail to use these the expericenced climbers are not going to
    be very sympathetic.
     
    Brian McCauley, Sep 2, 2004
    #3
  4. In article <>,
    Madhusudan Singh <> wrote:
    ....
    > How does one embed an awk command in perl ? I tried :


    The same way one embeds a few extra greps, seds, cuts, pastes, and, don't
    forget, one or more UUOCs.
     
    Kenny McCormack, Sep 2, 2004
    #4
  5. Madhusudan Singh

    Bob Harris Guest

    In article <>,
    Madhusudan Singh <> wrote:

    > Hi
    >
    > I am kind of new to perl (dabbled in it a little bit 4-5 years ago,
    > but
    > never needed it till now - I am writing an application).
    >
    > I need to extract some info from /sbin/iwconfig eth1 :
    >
    > $ /sbin/iwconfig eth1 | grep "Link"
    >
    > Link Quality:90/92 Signal level:-8 dBm Noise level:-148 dBm
    > (updated)
    >
    > I want to extract 90/92 from the stuff above. How do I code it ?
    >
    > If it is possible to use awk to do the above ('{print $2}' yields
    > "Quality:90/92", when I need to extract 90/92 or even better 90 and 92
    > separately, that is also ok.
    >
    > How does one embed an awk command in perl ? I tried :
    >
    > $info=`/sbin/iwconfig eth1 | grep "Link" | awk '{print $2}'`; but it
    > did
    > not work (just reproduced o/p as if the second pipe were not even present).
    >
    > Thanks.


    First, perl has excellent pattern matching and data manipulation
    facilities, so I don't know why you are using awk to grab information.
    But to answer the awk part of the question.

    $info=`/sbin/iwconfig eth1 | awk '/Link/ {split(\$2,a,":"); print a[2]}`

    Notice I backquoted the $, as perl might be gobbling it up looking to
    substitute a perl variable.

    Also watch out for a trailing Newline, unless you change the print a[2]
    to a printf "%s",a[2]

    But I think there are much more Perl'ish ways to capture this
    information without using awk, I just do not happen to be a wiz at Perl
    :)

    Good luck.

    Bob Harris
     
    Bob Harris, Sep 2, 2004
    #5
  6. Tad McClellan wrote:


    > Use a m// in list context instead of the above then:
    >
    > my($first, $second) = $_ =~ m!Link Quality:(\d+)/(\d+)!;


    Thanks for the help :)
     
    Madhusudan Singh, Sep 3, 2004
    #6
  7. Brian McCauley wrote:

    > Madhusudan Singh wrote:
    >
    >> I need to extract some info from /sbin/iwconfig eth1 :
    >>
    >> $ /sbin/iwconfig eth1 | grep "Link"
    >>
    >> Link Quality:90/92 Signal level:-8 dBm Noise level:-148 dBm
    >> (updated)
    >>
    >> I want to extract 90/92 from the stuff above. How do I code it ?

    >
    > my ($link_quality) = `/sbin/iwconfig eth1` =~ /Link Quality:(\S+)/;
    >
    >> [...] even better 90 and 92 separately [...]


    Oops didn't see that bit. Like Tad said...

    my ($x,$y) = `/sbin/iwconfig eth1` =~ /Link Quality:(\d+)\/(\d+)/;

    Note: Tad will replace the // match with alternate delimiters even to
    save a single \/. I consider this actually, on ballance, makes for
    harder to read code. I don't change the delimiters unless there would
    be a lot of \/.
     
    Brian McCauley, Sep 3, 2004
    #7
  8. Madhusudan Singh

    William Park Guest

    In <comp.lang.awk> Madhusudan Singh <> wrote:
    > Hi
    >
    > I am kind of new to perl (dabbled in it a little bit 4-5 years
    > ago, but never needed it till now - I am writing an
    > application).
    >
    > I need to extract some info from /sbin/iwconfig eth1 :
    >
    > $ /sbin/iwconfig eth1 | grep "Link"
    >
    > Link Quality:90/92 Signal level:-8 dBm Noise level:-148
    > dBm (updated)
    >
    > I want to extract 90/92 from the stuff above. How do I code it
    > ?
    >
    > If it is possible to use awk to do the above ('{print $2}'
    > yields "Quality:90/92", when I need to extract 90/92 or even
    > better 90 and 92 separately, that is also ok.
    >
    > How does one embed an awk command in perl ? I tried :
    >
    > $info=`/sbin/iwconfig eth1 | grep "Link" | awk '{print $2}'`;
    > but it did not work (just reproduced o/p as if the second pipe
    > were not even present).


    If you're adventurous, use shell.

    1. iwconfig eth1 | grep Link | while IFS=' :/' read a b c d e; do
    echo $c $d
    done

    2. line=`iwconfig eth1 | grep Link`
    sscanf "$line" ' Link Quality:%[0-9]/%[0-9]' c d
    echo $c $d

    --
    William Park <>
    Open Geometry Consulting, Toronto, Canada
     
    William Park, Sep 3, 2004
    #8
    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. DelphiDude
    Replies:
    3
    Views:
    1,176
  2. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    244
    Marc Bissonnette
    Jan 13, 2004
  3. Vandana

    Perl Pattern Matching-- Newbie

    Vandana, Jan 19, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    113
    Tad McClellan
    Jan 19, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    240
    Xicheng Jia
    May 3, 2007
  5. vivek_12315

    Pattern matching [newbie]

    vivek_12315, Feb 13, 2013, in forum: Perl Misc
    Replies:
    3
    Views:
    195
    J├╝rgen Exner
    Feb 13, 2013
Loading...

Share This Page