问题描述
我需要使用带有Power-shell的grep报告关键字的出现情况,并在可能的情况下以csv格式导出结果。
我需要与此命令类似的命令(在Powershell中对我不起作用)
cat .\PlaySound.txt | grep "Keyword" | grep $(date --date="@$(($(date +%s) - 3600))" "+%d/%b/%Y:%H") | wc -l
例如:
- 17:20 SomeKeyword:13
- 17:21 SomeKeyword:23
- 17:22 SomeKeyword:18
- 17:23 SomeKeyword:11
- ...
- 18:20 SomeKeyword:10
解决方法
我假设主要问题是生成和格式化时间戳?
在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