从实时(更新)日志文件使用Windows PowerShell筛选字符串 [英] filter string from live (updating) log file using windows powershell
问题描述
我有一台计算机正在从串口上的设备记录事件。 Putty将它们记录到文件中。
我曾经在Linux机器上运行过这个程序。基本上会 tail -f event.log>> script.sh
这是* nix脚本
#!/ bin / bash
outfile = sras_pages.log
while read -r line
do
IFS =''read -ra split <<< $ line
[[$ {split [1]} == AC / BATT_PWR]]&& echo$ {line:9:29}
[[$ {split [1]} == COMM-FAULT]]&& echo$ {line:9:29}
done>> $ outfile
基本上我只想转发包含上面两个字符串的字符位置9-29到一个由寻呼终端监控的文件。
现在Linux选项已经不存在了,而且我被困在了窗口中。我只是刚刚了解Windows PowerShell,并试图通过我在网上找到的例子来通过我的方式。
我开始这个:
Get-Content -Path C:\temp\SRAS\alarm.log -tail 1 -Wait
这给我尾巴-f
功能I有了Linux。问题是我不知道如何将此命令的输出提供给另一个PowerShell脚本。
然后我拼凑在一起
$ p = @(AC / BATT_PWR,COMM-FAULT)
Get-Content -Path C :\temp\SRAS\alarm.log -tail 1 -Wait | Select-String -Pattern $ p -SimpleMatch | Set-Content C:\temp\SRAS\sras_pages.log
文件sras_pages.log只写入一次我 Ctrl + C 在powershell停止运行。
我需要的是监视一个定期更新的日志文件,以及任何匹配字符串的事件发生时都会附加到一个单独的文件。
I don对于PowerShell有足够的了解,并且正在寻找一些帮助。
$ b $
非常感谢。
Add-Content
附加到文件,这将更适合于随着时间的推移将继续写入的脚本,但就像 Set-Content ,它会锁定文件并在脚本完成或取消时刷新。
$ b Out-File
但是也支持附加,并且不锁定文件。所以你可以尝试用
Out来替换 Set-Content
-File -FilePathC:\temp\SRAS\sras_pages.log-Append
I have a computer that is logging events from a device on a serial port. Putty is logging them to a file.
I used to have this running on a Linux machine. Basically would tail -f event.log >> script.sh
This was the *nix script
#!/bin/bash
outfile=sras_pages.log
while read -r line
do
IFS=' ' read -ra split <<< "$line"
[[ ${split[1]} == AC/BATT_PWR ]] && echo "${line:9:29}"
[[ ${split[1]} == COMM-FAULT ]] && echo "${line:9:29}"
done >> "$outfile"
Basically I want to forward only character positions 9-29 that contain the two strings above to a file that is monitored by a paging terminal.
The Linux option is gone now and I'm stuck with windows. I've only just learned about windows PowerShell and have been trying to muck my way through it via examples i find online.
I started with this:
Get-Content -Path C:\temp\SRAS\alarm.log -tail 1 -Wait
Which gives me the tail -f
functionality I got with linux. The problem is I can't figure out how to feed the output of this command into another PowerShell script.
I then cobbled this together
$p = @("AC/BATT_PWR","COMM-FAULT")
Get-Content -Path C:\temp\SRAS\alarm.log -tail 1 -Wait | Select-String -Pattern $p -SimpleMatch | Set-Content C:\temp\SRAS\sras_pages.log
Which works, EXCEPT the file sras_pages.log is only written to once I Ctrl+C in the powershell to stop it from running.
What I need is to monitor a log file that is updating regularly, and any events that match a string to be appended to a separate file when they happen.
I don't know enough about powershell to do this and am looking for some help.
Much appreciated.
解决方案 Set-Content
overwrites the file's contents, so it waits for all the content before it flushes and writes everything to the file. Add-Content
appends to the file, which would fit better for a script that will continue to write over time, but just like Set-Content
, it locks the file and flushes when the script is done or cancelled.
Out-File
however also supports appending, and it does not lock the file. So you could try replacing your Set-Content
command with:
Out-File -FilePath "C:\temp\SRAS\sras_pages.log" -Append
这篇关于从实时(更新)日志文件使用Windows PowerShell筛选字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!