Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

grep in line and print

by justbow (Initiate)
on Jan 24, 2013 at 02:16 UTC ( #1015063=perlquestion: print w/ replies, xml ) Need Help??
justbow has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i have input like this :
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 :
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 ?

Comment on grep in line and print
Select or Download Code
Re: grep in line and print
by roboticus (Canon) on Jan 24, 2013 at 02:23 UTC

    justbow:

    That code certainly doesn't do what you think it does. Add a print $line,"\n"; statement just after foreach to see why.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      do u mean like this :
      foreach print $line,"\n"; (@macall) {
      it will be error :D

        justbow:

        No, I meant like this:

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

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: grep in line and print
by Athanasius (Monsignor) on Jan 24, 2013 at 02:31 UTC

    Hello justbow, and welcome to the Monastery!

    Perhaps this is what you’re looking for:

    #! perl use strict; use warnings; while (my $line = <DATA>) { if ($line =~ /C4:46:19:75:C1:55/) { print "ID = ", (split /\s+/, $line)[0], "\n"; } } __DATA__ 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*

    Output:

    12:28 >perl 495_SoPW.pl ID = 1-1 12:28 >

    Update: The following is probably clearer:

    #! perl use strict; use warnings; while (my $line = <DATA>) { chomp $line; my @recs = split /\s+/, $line; if ($recs[2] eq 'C4:46:19:75:C1:55') { print "ID = ", $recs[0], "\n"; } } __DATA__ 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*

    (Same output.)

    See split.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hello Athanasius, Thanks for the suggestion, But im still getting the error :D I'm trying the code :
      #! perl use strict; use warnings; $allmac = "~/temp/allmac.txt"; print $allmac; while (my $line = $allmac) { if ($line =~ /C4:46:19:75:C1:55/) { $id = (split /\s+/, $line)[0]; print "ID = $id\n"; } }
      And getting error :
      Global symbol "$allmac" requires explicit package name at ./filtermac. +pl line 5. Global symbol "$allmac" requires explicit package name at ./filtermac. +pl line 6. Global symbol "$allmac" requires explicit package name at ./filtermac. +pl line 7. Global symbol "$id" requires explicit package name at ./filtermac.pl l +ine 11. Global symbol "$id" requires explicit package name at ./filtermac.pl l +ine 12. Execution of ./filtermac.pl aborted due to compilation errors.

        With use strict in effect, you need to declare your variable names:

        my $allmac = ... ... my $id = ...

        But the line:

        while (my $line = $allmac)

        won’t work: it’s assigning the file name to $line, but it needs to call readline on a file handle, like so:

        open (my $fh, '<', $allmac) or die "Can't open file '$allmac' for read +ing: $!"; while (my $line = <$fh>) { ....

        Applying the diamond operator to a filehandle: <$fh> is the standard way to call readline in Perl. See readline and I/O Operators; also open and perlopentut.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015063]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-08-20 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (116 votes), past polls