Powershell Select-字符串
Powershell Select-String
我需要你在 PowerShell 方面的帮助。
我需要 Select- 具有固定日期(变量)的字符串。 & 将内容设置为 result.txt
示例:$Date = "01.07.2020"
但我还需要 select 字符串,其日期低于我在变量中写入的日期。
我的代码:Get-Content -Path log.txt | Select-String "?????" | Set-Content $result.txt
- 在 log.txt 中,我有很多字符串,例如“Creation date 01.07.2020”; “创建日期 01.06.2020”
123.txt
Creation date 01.07.2020
Creation date 02.05.2020
Creation date 01.06.2020
Creation date 28.08.2020
示例脚本
$file = "C:\Users\userprofile\Desktop\test3.txt"
$regexpattern = "\d{2}\.\d{2}\.\d{4}"
$content = Get-Content $file | Where-object { $_ -match $regexpattern}
foreach($line in $content){
$line.Substring(13,11)
}
我使用正则表达式找到了你想要输出的行。只有当内容与我们的正则表达式匹配时,我们才会获取内容,然后对于我们找到的每一行,我都使用子字符串来提取日期。如果你愿意,你也可以为此组合一个正则表达式。由于我们知道这些行具有相同数量的字符,因此使用 substring 函数是安全的。
如果您想要将该输出到文件,只需找到 $line.Substring(13,11)
然后在其后添加 | Out-file "C:\Users\userprofile\desktop\test\output.txt" -append
.
我需要你在 PowerShell 方面的帮助。
我需要 Select- 具有固定日期(变量)的字符串。 & 将内容设置为 result.txt
示例:$Date = "01.07.2020"
但我还需要 select 字符串,其日期低于我在变量中写入的日期。
我的代码:Get-Content -Path log.txt | Select-String "?????" | Set-Content $result.txt
- 在 log.txt 中,我有很多字符串,例如“Creation date 01.07.2020”; “创建日期 01.06.2020”
123.txt
Creation date 01.07.2020
Creation date 02.05.2020
Creation date 01.06.2020
Creation date 28.08.2020
示例脚本
$file = "C:\Users\userprofile\Desktop\test3.txt"
$regexpattern = "\d{2}\.\d{2}\.\d{4}"
$content = Get-Content $file | Where-object { $_ -match $regexpattern}
foreach($line in $content){
$line.Substring(13,11)
}
我使用正则表达式找到了你想要输出的行。只有当内容与我们的正则表达式匹配时,我们才会获取内容,然后对于我们找到的每一行,我都使用子字符串来提取日期。如果你愿意,你也可以为此组合一个正则表达式。由于我们知道这些行具有相同数量的字符,因此使用 substring 函数是安全的。
如果您想要将该输出到文件,只需找到 $line.Substring(13,11)
然后在其后添加 | Out-file "C:\Users\userprofile\desktop\test\output.txt" -append
.