Home: Perl Programming Help: Win32 Programming Help:
grep inline use perl



justbow
New User

Jan 23, 2013, 6:04 PM


Views: 19420
grep inline use perl

Hi,

i have input like this :

Quote
SS-ID VLAN MAC TIME IP RSSI MODE UAPSD BW GI WMOS DHCP IDENTITY
----- ---- --- ---- -- ---- ---- ----- -- -- ---- ---- --------
1-1 0 C4:46:19:75:C1:55 23m 192.168.5.253 -24 bgn no 20 S 4.9 ack*
1-2 0 5C:57:C8:69:8C:1E 3s 192.168.5.254 -38 bg no 20 L 4.8 ack*



I want the output is "1-1" by grep in line by MAC Address

So far i use this scripts, but doesn't work :


Quote
my @macall = qw(print @input);
foreach $line (@macall) {
if ($line =~ /C4:46:19:75:C1:55:\s+(\S+)/) {
$id = $1;
}
}
print "ID = $id";
return (0);


Anybody can help ?


FishMonger
Veteran / Moderator

Jan 24, 2013, 7:09 AM


Views: 19395
Re: [justbow] grep inline use perl

Saying "but doesn't work" is an extremely poor problem statement.

You should always explain in what way it does not work as expected?

What are you expecting this to accomplish?

Quote

Code
my @macall = qw(print @input);



Try this test script to see if it does what you need.

Code
use strict; 
use warnings;

my $src_mac = 'C4:46:19:75:C1:55';
my $input = <<INPUT;
SS-ID VLAN MAC TIME IP RSSI MODE UAPSD BW GI WMOS DHCP IDENTITY
----- ---- --- ---- -- ---- ---- ----- -- -- ---- ---- --------
1-1 0 C4:46:19:75:C1:55 23m 192.168.5.253 -24 bgn no 20 S 4.9 ack*
1-2 0 5C:57:C8:69:8C:1E 3s 192.168.5.254 -38 bg no 20 L 4.8 ack*
INPUT

foreach my $line ( split /\n/, $input ) {
my ($id, $mac) = (split ' ', $line)[0,2];
if ( uc($mac) eq $src_mac ) {
print "ID = $id\n";
}
}



BillKSmith
Veteran

Jan 24, 2013, 7:12 AM


Views: 19393
Re: [justbow] grep inline use perl

I could offfer more help if you posted a complete program, but let me try.

I am not sure what your first line is intended to do. It does not seem to make any sense. Your foreach probably should be for each line of input.

Your regular expression has two major errors. It includes a colon after the 55. This will not match any of your data lines. The expression (\S+) is in the wrong place to return the id. (It will return 23m after you fix the colon)

Your return is invalid in a main program. Use exit if the return code is important.

Post your code with code (not quote) tags. They will preserve indenting.


Please repost your complete program !
Good Luck,
Bill