在 Powershell 中使用 Grep 每分钟报告一次关键字出现

Reporting keyword occurrence every minutes using Grep in Powershell

我需要使用带有 power-shell 的 grep 报告关键字出现情况,并尽可能以 csv 格式导出结果。

我需要类似这个的命令(它对我无效shell)

cat .\PlaySound.txt | grep "Keyword" | grep $(date --date="@$(($(date +%s) - 3600))" "+%d/%b/%Y:%H") | wc -l

例如:

我假设主要问题是生成和格式化时间戳?

在 PowerShell 中,等同于:

Get-Date (Get-Date).AddHours(-1) -UFormat "+%d/%b/%Y:%H"

因此,要重新创建整个管道,我们可以这样做:

$timestamp = Get-Date (Get-Date).AddHours(-1) -UFormat "+%d/%b/%Y:%H"
Get-Content .\PlaySound.txt |Where-Object {$_ -match 'Keyword' -and $_ -match $timestamp} |Measure-Object -Line |Select-Object -Expand Lines