【发布时间】:2016-12-21 08:32:28
【问题描述】:
我正在尝试从文件中提取出现在特定单词模式之间的句子。目的是从文件中提取出现在第一对“GO”单词之间的句子。这里实现的逻辑是根据单词'GO'分割文件,然后打印数组的第二个元素(本例中以SET开头的句子)。但是,PowerShell 无法识别分隔符 (GO);相反,它似乎将“新行”识别为分隔符,并正在打印第二句。
请注意,我需要读取文件,然后完成提取。
文件内容
Home address "TJ One way"
Office address "C company Two way"
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
Home address "TJ One way"
Office address "C company Two way"
GO
:on error exit
GO
我的代码
$path = 'D:\Scripts'
$deltaFile = 'GoSampleFile.txt'
$modifiedDelta = 'GoSampleFile1.txt'
New-Item -path $path -Name $modifiedDelta -ItemType file -Force
#Split for each appearing GO, after escaping the double quotes
(Get-Content $path'\'$deltaFile).replace('"', '`"') | Set-Content $path'\'$modifiedDelta
$separator = 'GO'
$modifiedDeltaString = Get-Content $path'\'$modifiedDelta
#Write-Host $modifiedDeltaString
#Write-Host $separator
$goArray = $modifiedDeltaString -split "GO", 0, "SimpleMatch"
Write-Output $goArray[1]
#Housekeeping of the temporary file
Remove-Item $path'\'$modifiedDelta
【问题讨论】:
标签: string powershell split