search keyword in log and read that line
Hi,

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.
January 17th, 2014 8:45am

Hi,

Give this a try:

Get-Content .\logFile.txt | ForEach { If ( $_.contains('TOTAL') ) { $_ } }

Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 8:50am

Hello.

Use this command:

Get-Content E:\tem\filelog.log | Select-String -SimpleMatch -Pattern "Total"

Good luck)

January 17th, 2014 9:04am

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.
Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 10:03am

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') ) { $_ } }

January 17th, 2014 10:08am

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




Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 10:10am

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 :(
  • Edited by TekFinder 20 hours 22 minutes ago
January 17th, 2014 10:14am

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
Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 10:19am

Hi Mike,

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
January 17th, 2014 10:48am

Sure, that's a perfectly good solution as well. Here's a bit more information on Select-String:

http://ss64.com/ps/select-string.html

Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 11:06am

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




January 17th, 2014 6:05pm

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 :(
  • Edited by TekFinder Friday, January 17, 2014 3:10 PM
Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 6:09pm

Hi Mike,

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
January 17th, 2014 6:43pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics