我必须从文本文件中提取一些数据,但在此之前,我需要使用以下规则连接几行:如果行以空格开头,那么它应该与前面的行合并,该行不以空格开头。对以空格开头的所有连续行执行此操作。
虽然我已经为该它和样本一起在这里找到创建了regex,并且替换面板显示了所需的输出,但我无法将它合并到PowerShell脚本中:
Get-ChildItem .\abc.txt | ForEach-Object {
$content = Get-Content $_.FullName
#remove all lines starting with *
$content | Select-String -Pattern "\*" -NotMatch |
%{ $_ -replace '(\s+^\n?\s)', "" } |
Set-Content $_.FullName
}
当regex运行时,我无法理解如何使它工作。
发布于 2017-06-22 04:05:34
为了与-replace
操作符合并行,您需要一个字符串中的文件内容。在PowerShell v3和更新程序中,您可以通过使用参数-Raw
调用Get-Content
来实现这一点。
(Get-Content $_.FullName -Raw) -replace '\r?\n\s+' | Set-Content $_.FullName
在较早的PowerShell版本上,通过Out-String
调用Get-Content
的输出
(Get-Content $_.FullName | Out-String) -replace '\r?\n\s+' | Set-Content $_.FullName
https://stackoverflow.com/questions/44698722
复制