in reply to Re^5: need to remove the extra line in regular expression
in thread need to remove the extra line in regular expression

I am not getting where to check on interface name matching, can you please help me.
  • Comment on Re^6: need to remove the extra line in regular expression

Replies are listed 'Best First'.
Re^7: need to remove the extra line in regular expression
by aitap (Curate) on Aug 23, 2012 at 08:51 UTC
    Check it on the same line where the $interface gets assigned. Remove the print statement from your check (move it outside the if(...){...} block).
    Sorry if my advice was wrong.
      Check is not happening please let me know where is the mistake.
      open FILE,"config.txt" or die "cannot open file : $!"; my $interface; # declare the variable while(<FILE>) { if (/Link encap/){ # if there are non-space symbols on the start of the line, # consider it interface name $interface = $1 if /^([\S]+)/; # $1 means first matched () group of s +ymbols # a group of numeric symbols or dots after "inet addr:" # is considered an IP-address print "$interface => $1\n" if /inet addr:([0-9.]+)/; # use /inet6? addr: ?([0-9a-f.]+)/ if you want to match IPv6-addresse +s } print; }

        Your mistake is that you do the check for "Link encap" string for both of the lines (first containing the interface name and second containing the address) while you need to do it only for the first line (as long as there is no "Link encap" in the line where the address is specified).

        Do you understand this code?

        Sorry if my advice was wrong.
      Yes I understand the code and This is my understanding the code. while(<FILE>) { =In this I am reading the complete file with file handle. $interface = $1 if /^([\S]+)/; =this line will check the one or more non whitspace char and if ti fou +nd put the value in $1 and assign to $interface. print "$interface => $1\n" if /inet addr:([0-9.]+)/; =this line will check the address and if it found put the value in $1. But I am not able to check the condition on the first line.
        In this I am reading the complete file with file handle.
        No, you read it line by line, and the code inside the while(){} loop gets run once for every line in the text you read.
        But I am not able to check the condition on the first line.
        Just don't touch the second check in any way. Modify the first check, not the whole loop.
        Sorry if my advice was wrong.