I have a log, where I am searching for a keyword, then read the whole line.
Here i am looking for a the keyword "TOTAL" which appears in different places in different logs. So i cannot use 2nd line from bottom or 5th line from UP etc. I need to search for the word "TOTAL" anywhere in the log and must read the whole line. Any help please?
example 1:
01/14/14 07:10:00 Log ----------------------------------
01/14/14 07:10:00 Log Start \\STMSRV01\APTEK$\APTEK.exe [1.1.15 64-bit] -l\\STMSRV01\APTEK$\APTEK.log -iE:\certs\\1F011314.ps1 -x\\STMSRV01\APTEK$\1F_spec.xml -oaxml -cE:\certs\\ -dn
01/14/14 07:10:00 Log Spec= \\STMSRV01\APTEK$\1F_spec.xml [10/30/2013 22:15:01 39,436 bytes]
01/14/14 07:10:00 Log Data= E:\certs\\1F011314.ps1 [01/14/2014 07:00:05 714,175 bytes]
01/14/14 07:10:00 Log APVersionNumber= 3.0.0.0
01/14/14 07:10:01 TOTAL 2 3 9 10
01/14/14 07:10:01 Log Script compilation time: 00:00:00.1248072
01/14/14 07:10:02 Log Data translation duration: 00:00:01.0587602.
01/14/14 07:10:02 Log Data translation duration: 00:00:01.0587602.
01/14/14 07:10:02 Log Process Completed.
example 2:
01/14/14 07:10:00 Log ----------------------------------
01/14/14 07:10:00 Log Start \\STMSRV01\APTEK$\APTEK.exe [1.1.15 64-bit] -l\\STMSRV01\APTEK$\APTEK.log -iE:\certs\\1F011314.ps1 -x\\STMSRV01\APTEK$\1F_spec.xml -oaxml -cE:\certs\\ -dn
01/14/14 07:10:00 Log Spec= \\STMSRV01\APTEK$\1F_spec.xml [10/30/2013 22:15:01 39,436 bytes]
01/14/14 07:10:00 Log Data= E:\certs\\1F011314.ps1 [01/14/2014 07:00:05 714,175 bytes]
01/14/14 07:10:00 Log APVersionNumber= 3.0.0.0
01/14/14 07:10:01 Log Script compilation time: 00:00:00.1248072
01/14/14 07:10:02 Log Data translation duration: 00:00:01.0587602.
01/14/14 07:10:02 TOTAL 98 54 22 1
01/14/14 07:10:02 Log Process Completed.
Hi,
Give this a try:
Get-Content .\logFile.txt | ForEach { If ( $_.contains('TOTAL') ) { $_ } }
Hello.
Use this command:
Get-Content E:\tem\filelog.log | Select-String -SimpleMatch -Pattern "Total"
Good luck)
Hi this working perfectly. Thanks. One addition is, Is there anyway to force search for the keyword in CAPITAL? So the word "TOTAL" will only be accepted, but all other "Total" or "total" will be ignored.
Sure, that can be done:
Get-Content .\logFile2.txt | ForEach { If ( $_.ToUpper().contains('TOTAL') ) { $_ } }
Get-Content E:\temp\filelog.log | Select-String -SimpleMatch -Pattern "total" -CaseSensitive:$false
Get-Content E:\temp\filelog.log | ?{$_ -like "*total*"} #case insensetive
Get-Content E:\temp\filelog.log | ?{$_ -clike "*TOTAL*"} #case sensitive
- Edited by Kolomiets Sergei 20 hours 0 minutes ago
- Marked as answer by TekFinder 19 hours 53 minutes ago
- Edited by TekFinder 20 hours 22 minutes ago
Hi, this not working, because it's first making all "total" CAPITAL then matching with my keyword "TOTAL", finally bringing all 3 TOTAL, Total & total exist in my log :(
=]
I read that wrong.
The first bit of code I posted will only look for TOTAL in capitals. I've added in Total and total to your logfile example and I'm only getting the TOTAL lines returned.
PS C:\Scripts\PowerShell Scripts\Misc Testing\1-17-2014> Get-Content .\logFile2.txt | ForEach { If ( $_.ToUpper().contains('TOTAL') ) { $_ } } 01/14/14 07:10:01 TOTAL 2 3 9 10 01/14/14 07:10:02 TOTAL 98 54 22 1 01/14/14 07:10:02 total 98 54 22 1 01/14/14 07:10:02 Total 98 54 22 1 PS C:\Scripts\PowerShell Scripts\Misc Testing\1-17-2014> Get-Content .\logFile2.txt | ForEach { If ( $_.contains('TOTAL') ) { $_ } } 01/14/14 07:10:01 TOTAL 2 3 9 10 01/14/14 07:10:02 TOTAL 98 54 22 1
I saw that your first solution worked, but actually I wanted to be sure this will not fail in any situation as one of my further process will fail if i don't get it right, so was looking for if there is anyway to force to search for real CAPITAL. Just saw a suggestion from Kolomiets Sergei in this thread, where I can control case-sensitivity. Do you suggest to use this instead and it's efficient in any situation?
Get-Content E:\temp\filelog.log | Select-String -SimpleMatch -Pattern "TOTAL" -CaseSensitive:$true
- Edited by TekFinder 19 hours 46 minutes ago
Sure, that's a perfectly good solution as well. Here's a bit more information on Select-String:
Get-Content E:\temp\filelog.log | Select-String -SimpleMatch -Pattern "total" -CaseSensitive:$false
Get-Content E:\temp\filelog.log | ?{$_ -like "*total*"} #case insensetive
Get-Content E:\temp\filelog.log | ?{$_ -clike "*TOTAL*"} #case sensitive
- Edited by Kolomiets Sergei Friday, January 17, 2014 3:32 PM
- Marked as answer by TekFinder Friday, January 17, 2014 3:39 PM
- Edited by TekFinder Friday, January 17, 2014 3:10 PM
I saw that your first solution worked, but actually I wanted to be sure this will not fail in any situation as one of my further process will fail if i don't get it right, so was looking for if there is anyway to force to search for real CAPITAL. Just saw a suggestion from Kolomiets Sergei in this thread, where I can control case-sensitivity. Do you suggest to use this instead and it's efficient in any situation?
Get-Content E:\temp\filelog.log | Select-String -SimpleMatch -Pattern "TOTAL" -CaseSensitive:$true
- Edited by TekFinder Friday, January 17, 2014 3:46 PM