繁体   English   中英

如何在 Select-String 中使用正则表达式开头的行?

[英]How to use regex beginning of line with Select-String?

我有以下代码,

Write-Output "$(Get-Content $alerfile | Select-String $(Get-Date (Get-Date).AddDays(-1) -Format $filtdate) -Context 0,1000000)"

我只需要返回行首的字符串,我试过:

Write-Output "$(Get-Content $alerfile | Select-String ^$(Get-Date (Get-Date).AddDays(-1) -Format $filtdate) -Context 0,1000000)"

但它不起作用。

正如Lee_Dailey评论的那样,您需要删除Write-Output 此外,您对模式中的日期做得过火了。

接下来, Select-String cmdlet 还有一个-Path参数,因此您不需要Get-Content

我建议这样的事情:

# insert the datetime format you need here; this is just an example
$filtdate = 'yyyyMMdd HH:mm:ss'
# get yesterdays date formatted using the $filtdate template above
$refdate  = (Get-Date).AddDays(-1).ToString($filtdate)
# find the string beginning with that formatted date and return the results
Select-String -Path $alerfile -Pattern "^$refdate" -Context 0,1000000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM