Newbie needs help on pattern matching

  • Thread starter Madhusudan Singh
  • Start date
M

Madhusudan Singh

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.
 
T

Tad McClellan

Madhusudan Singh said:
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. :)
 
B

Brian McCauley

Madhusudan said:
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.
 
K

Kenny McCormack

Madhusudan Singh said:
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.
 
B

Bob Harris

Madhusudan Singh said:
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
 
M

Madhusudan Singh

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 :)
 
B

Brian McCauley

Brian said:
Madhusudan said:
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 \/.
 
W

William Park

In said:
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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top