Click here to Skip to main content
14,661,175 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a text file that is auto-generated by an application when it is started. I want powershell to read that text file continously(even while another application writes info into that file) and stop reading once it finds a word started. Once it finds that specific word by making it as an end of file, it should find and display the lines containing the words I specify.

In short: 1) Powershell to read the text file continuously until it finds a word started
2) Find for some words(eg:info) from start of file upto started and display the whole line containing that particular word(info).

Any help would be really appreciated..
Posted
Comments
Richard MacCutchan 2-Jul-13 5:44am
   
What help? If you are expecting someone to write the script for you you will be disappointed. Show what you have done so far and where you are stuck.
Vysakh Venugopal 2-Jul-13 6:20am
   
@Richard MacCutchan:
This is my code.
key.txt contains 3 words(error,info,warning) and read.log contains many words including the three specified and the word "started".
Eventhough warning and information comes after "started", its displaying me all the lines which includes the words specified in key.txt

I want in such a way that if it finds a word "started", then it should stop searching further. Any idea on this??

$key = Get-Content D:\ReadLog\key.txt
Get-Content D:\ReadLog\read.log -wait |
ForEach-Object {
foreach($word in $key){
if($_ -match $word){
$_
}
else
{
Select-String -Path D:\ReadLog\read.log -pattern started
write-host "started"
}
}
}

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Found out the solution:

$key = Get-Content D:\ReadLog\key.txt
Get-Content D:\ReadLog\read.log -wait |
ForEach-Object {
foreach($word in $key){
if($_ -match "started")
{
write-host "started"
exit
}
if($_ -match $word){
$_
}
}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100