Ask a questionAsk a question
 

AnswerQuery a special string

  • Thursday, October 29, 2009 7:14 AMGlaziz.Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    If i have a log file just like below.

    ping 10.132.2.1 ...
    ping .. Received packet from 10.132.2.1  Time : 0
    ping .. Received packet from 10.132.2.1  Time : 0
    ping 10.132.2.2 ...
    ping .. Error: Request timed out
    ping .. Error: Request timed out
    ping 10.132.2.3 ...
    ping .. Error: Request timed out
    ping .. Error: Request timed out
    ping 10.132.2.4 ...
    ping .. Received packet from 10.132.2.4  Time : 4
    ping .. Received packet from 10.132.2.4  Time : 2
    ping 10.132.2.5 ...
    ping .. Received packet from 10.132.2.5  Time : 1
    ping .. Received packet from 10.132.2.5  Time : 0
    ping 10.132.2.6 ...
    ping .. Received packet from 10.132.2.6  Time : 1
    ping .. Received packet from 10.132.2.6  Time : 1
    ping 10.132.2.7 ...
    ping .. Received packet from 10.132.2.7  Time : 1
    ping .. Received packet from 10.132.2.7  Time : 0
    ping 10.132.2.8 ...
    ping .. Error: Request timed out
    ping .. Error: Request timed out

    Now, i want to find out IP which timed out machine. I.E. program can catch up 
    10.132.2.2
    10.132.2.3
    10.132.2.8

    Could someone help me?
    Thanks,

Answers

  • Thursday, October 29, 2009 8:32 AMVadims PodansMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    sure:

    # retrieve log file content
    $log = gc desktop\log.txt 
    # looping each line
    for ($n = 0; $n -lt $log.count-1; $n++) {
    # check if current string contains an IP address. If it is, capture this by using IP-address pattern
        if ($log[$n] -match "ping ([\d\.]+) \.{3}") {
    # if IP address is caputured, writing this address to variable
            $ip = $matches[1]
    # check if next line contains timed out string by exact equality. If
    # ping is timed out, return IP address and looping to next line...
            if ($log[$n+1] -eq "ping .. Error: Request timed out") {$ip}
        }    
    }

    http://www.sysadmins.lv
  • Friday, October 30, 2009 8:01 AMVadims PodansMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    \d - any digit
    \. - any dot
    [\d\.] - digit or dot
    [\d\.]+ - one or more digit or dot
    \.{3} - three dots
    http://www.sysadmins.lv

All Replies

  • Thursday, October 29, 2009 8:32 AMVadims PodansMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    sure:

    # retrieve log file content
    $log = gc desktop\log.txt 
    # looping each line
    for ($n = 0; $n -lt $log.count-1; $n++) {
    # check if current string contains an IP address. If it is, capture this by using IP-address pattern
        if ($log[$n] -match "ping ([\d\.]+) \.{3}") {
    # if IP address is caputured, writing this address to variable
            $ip = $matches[1]
    # check if next line contains timed out string by exact equality. If
    # ping is timed out, return IP address and looping to next line...
            if ($log[$n+1] -eq "ping .. Error: Request timed out") {$ip}
        }    
    }

    http://www.sysadmins.lv
  • Thursday, October 29, 2009 9:37 AMGlaziz.Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks, it is useful for me.
  • Thursday, October 29, 2009 9:55 AMGlaziz.Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I do not understand this line.  if ($log[$n] -match "ping ([\d\.]+) \.{3}") {

    could you explain it from ([\d\.]+) \.{3}
    or where i can find the relation syntax? I think this is very useful for me to deal further program.
    Thanks,

  • Friday, October 30, 2009 7:31 AMVincentK Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Its a regular Expression. I found this site very usefull in understanding those: http://www.regular-expressions.info/
  • Friday, October 30, 2009 8:01 AMVadims PodansMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    \d - any digit
    \. - any dot
    [\d\.] - digit or dot
    [\d\.]+ - one or more digit or dot
    \.{3} - three dots
    http://www.sysadmins.lv
  • Wednesday, November 04, 2009 7:52 AMGlaziz.Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    \d - any digit
    \. - any dot
    [\d\.] - digit or dot
    [\d\.]+ - one or more digit or dot
    \.{3} - three dots
    http://www.sysadmins.lv
    Hi,

    I follow your guide and adjust my script for try to match two strings. One likes ABC123 and the other likes CONABC.
    The first one format will be the first three letters are English alphabets and the last three letters are Arabic numeral.
    The second one format will be the first three letters are "CON" and the last three letters are English alphabets.
    I can draft "[a-z]\d{3}" for first one and "con[a-z]{3}" for seconds. However i want to combine two expression into one line.
    The program will can identify both format. Could someone help me? 
    Thanks,

  • Wednesday, November 04, 2009 8:30 AMVadims PodansMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    can you provide example string?


    http://www.sysadmins.lv