我试图使用PowerShell中的Select从包含变更日志条目的文本文档中提取行。我包括了一个样本,下面。
PS命令Select-String "REAPER.*(19|20)" "d:\reaper 6.x versions.txt"成功地提取了每个日志条目的第一行(例如“收割者”6.11-2020年5月24日),但我也需要每个条目中的第二行。
我尝试过Select-String "REAPER.*(19|20)\n.*" "d:\reaper 6.x versions.txt"和类似的,但它们返回空白或错误。
不知所措。
REAPER v6.11 - May 24, 2020
The Gone-Away World
Downloads:
Windows (12MB installer)
Windows x64 (13MB installer)
OS X Intel (18MB DMG)
OS X 64-bit Intel (20MB DMG)
OS X 64-bit Intel (20MB DMG, notarized for Catalina)
Linux x86_64 (11MB .tar.xz)
Linux i686 (11MB .tar.xz)
Linux armv7l (9MB .tar.xz)
Linux aarch64 (9MB .tar.xz)
Changes:
Appearance: add Theme Color Controls window for per-theme brightness/contrast/gamma/color adjustment
REAPER v6.10 - May 9, 2020
The Gone-Away World
Downloads:
Windows (12MB installer)
Windows x64 (13MB installer)
OS X Intel (18MB DMG)
OS X 64-bit Intel (20MB DMG)
OS X 64-bit Intel (20MB DMG, notarized for Catalina)
Linux x86_64 (11MB .tar.xz)
Linux i686 (11MB .tar.xz)
Linux armv7l (9MB .tar.xz)
Linux aarch64 (9MB .tar.xz)
Changes:
ARA: preserve edits when user applies timing changes to media or imports as MIDI发布于 2020-09-20 18:06:01
您可以使用
PS> Get-Content "d:\reaper 6.x versions.txt" -Raw | Select-String "REAPER.*(?:19|20)(?:\r?\n.*)?" -AllMatches | Foreach-Object { $_.Matches.Value }
REAPER v6.11 - May 24, 2020
The Gone-Away World
REAPER v6.10 - May 9, 2020
The Gone-Away World注意事项
Get-Content $file -Raw将整个文件读取为单个字符串,而不是一行数组,这样模式就可以在一次匹配操作中匹配多行。REAPER.*(?:19|20)(?:\r?\n.*)?模式将从REAPER匹配到19或20,然后是一个可选的CRLF或LF行结束序列,以及除换行符以外的任何零个或多个字符。见regex在线演示。
要将两个相邻的行输出为两个列,以便输出到CSV,您可以使用
Get-Content "d:\reaper 6.x versions.txt" -Raw |
Select-String "(REAPER.*(?:19|20))(?:\r?\n([^\r\n]*))?" -AllMatches |
Foreach {$_.Matches} |
Foreach { new-object psobject -Property @{Tool=$_.Groups[1];Name=$_.Groups[2]} } |
Select Tool,Name |
Export-Csv -NoTypeInformation "d:\reaper 6.x versions.csv"输出:
"Tool","Name"
"REAPER v6.11 - May 24, 2020","The Gone-Away World"
"REAPER v6.10 - May 9, 2020","The Gone-Away World"发布于 2020-09-20 18:54:09
使用-context如何?前0行,后面1行:
Select-String "REAPER.*(19|20)" file.txt -Context 0,1
> file.txt:1:REAPER v6.11 - May 24, 2020
file.txt:2:The Gone-Away Worldhttps://stackoverflow.com/questions/63982083
复制相似问题