Query a special string
- 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,- Changed TypeMarco ShawMVP, ModeratorThursday, October 29, 2009 10:11 AMquestion
Answers
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- Marked As Answer byMarco ShawMVP, ModeratorThursday, October 29, 2009 10:11 AM
- \d - any digit
\. - any dot
[\d\.] - digit or dot
[\d\.]+ - one or more digit or dot
\.{3} - three dots
http://www.sysadmins.lv- Proposed As Answer byVadims PodansMVPFriday, October 30, 2009 8:02 AM
- Marked As Answer byMervyn ZhangMSFT, ModeratorTuesday, November 03, 2009 5:28 AM
All Replies
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- Marked As Answer byMarco ShawMVP, ModeratorThursday, October 29, 2009 10:11 AM
- Thanks, it is useful for me.
- 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,
- Its a regular Expression. I found this site very usefull in understanding those: http://www.regular-expressions.info/
- Proposed As Answer byVadims PodansMVPFriday, October 30, 2009 8:02 AM
- \d - any digit
\. - any dot
[\d\.] - digit or dot
[\d\.]+ - one or more digit or dot
\.{3} - three dots
http://www.sysadmins.lv- Proposed As Answer byVadims PodansMVPFriday, October 30, 2009 8:02 AM
- Marked As Answer byMervyn ZhangMSFT, ModeratorTuesday, November 03, 2009 5:28 AM
\d - any digit
Hi,
\. - any dot
[\d\.] - digit or dot
[\d\.]+ - one or more digit or dot
\.{3} - three dots
http://www.sysadmins.lvI 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,can you provide example string?
http://www.sysadmins.lv

