Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie needs help on pattern matching

Reply
Thread Tools

Newbie needs help on pattern matching

 
 
Madhusudan Singh
Guest
Posts: n/a
 
      09-02-2004
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.
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-02-2004
Madhusudan Singh <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      09-02-2004


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.

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      09-02-2004
In article <(E-Mail Removed)>,
Madhusudan Singh <(E-Mail Removed)> 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.

 
Reply With Quote
 
Bob Harris
Guest
Posts: n/a
 
      09-02-2004
In article <(E-Mail Removed)>,
Madhusudan Singh <(E-Mail Removed)> 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
 
Reply With Quote
 
Madhusudan Singh
Guest
Posts: n/a
 
      09-03-2004
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
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-03-2004


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 \/.


 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      09-03-2004
In <comp.lang.awk> Madhusudan Singh <(E-Mail Removed)> 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 <(E-Mail Removed)>
Open Geometry Consulting, Toronto, Canada
 
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
Pattern matching [newbie] vivek_12315 Perl Misc 3 02-13-2013 08:54 PM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Perl Pattern Matching-- Newbie Vandana Perl Misc 4 01-19-2004 11:21 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
A newbie question on pattern matching DelphiDude Perl 3 07-26-2003 12:54 PM



Advertisments