#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    How to get the value of using condition check


    I have a file
    ------------------------------------------------------------------
    Port Speed Duplex Flow Ctrl Link Name
    ---- ----- -------- --TX-----RX-- ------ ------
    1 10000 full no no down 1
    2 10000 full no no down 2
    3 10000 full no no down 3
    4 10000 full no no down 4
    5 10000 full no no down 5
    6 10000 full no no down 6
    7 10000 full no no down 7
    8 10000 full no no down 8
    9 10000 full no no down 9
    10 10000 full no no down 10
    11 10000 full no no down 11
    12 10000 full no no down 12
    13 10000 full no no down 13
    14 10000 full no no down 14
    15 10000 full no no down 15
    16 10000 full no no down 16
    17 1G/10G full no no down 17
    18 1G/10G full no no down 18
    19 1000 full no no up 19
    20 10000 full no no down 20

    and my script is
    open FILE,"interfacelink.txt" or die "cannot open file : $!";
    my ($line, $ind, $port, $speed, $link);
    $ind=$line=~ /Link/;
    while (<FILE>) {

    $line = $_;
    if ( $line =~ /\bLink\b/ ) {
    print "port is ", $port, substr($line,1,2). ", ";
    print "speed is ", $speed, substr($line,7,5). ", ";
    print "link is ", $link, substr($line,42,4)."\n";

    }

    }

    when I run the script I got the output
    port is 19, speed is 1000, link is up

    I dont know how to check the condition like if the line contain
    "Link" in the first line then check the port link is up or down and if it is up then get the same out as above
    Last edited by lakshmikant; August 29th, 2012 at 08:11 AM. Reason: checking one more condition
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    The code you posted won't produce the results that you stated from the sample data you posted.

    Based on you sample data, I'd loop over the lines skipping any that don't start with a number, then use the split function to breakup the line into their separate fields. If you use an array slice, you can extract just the 3 needed fields instead of all 7.

IMN logo majestic logo threadwatch logo seochat tools logo