PowerShell:使用 String-Select 将表达式与文本文件匹配
PowerShell: Match Expression Against Text File Using String-Select
我正在尝试在 PowerShell 中使用 Select-String 从包含更改日志条目的文本文档中提取行。我在下面包含了一个示例。
PS 命令 Select-String "REAPER.*(19|20)" "d:\reaper 6.x versions.txt"
成功提取了每个日志条目的第一行(例如 REAPER v.6.11 - May 24, 2020)但是我还需要每个条目的第二行。
我试过 Select-String "REAPER.*(19|20)\n.*" "d:\reaper 6.x versions.txt"
和类似的,但它们 return 空白或错误。
难倒。
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
您可以使用
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 行结尾的可选序列以及任何零个或多个字符除了换行符。
要将相邻的两行输出为两列以输出到 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"
使用-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 World
我正在尝试在 PowerShell 中使用 Select-String 从包含更改日志条目的文本文档中提取行。我在下面包含了一个示例。
PS 命令 Select-String "REAPER.*(19|20)" "d:\reaper 6.x versions.txt"
成功提取了每个日志条目的第一行(例如 REAPER v.6.11 - May 24, 2020)但是我还需要每个条目的第二行。
我试过 Select-String "REAPER.*(19|20)\n.*" "d:\reaper 6.x versions.txt"
和类似的,但它们 return 空白或错误。
难倒。
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
您可以使用
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 行结尾的可选序列以及任何零个或多个字符除了换行符。
要将相邻的两行输出为两列以输出到 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"
使用-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 World