我想从文件中提取出现在单词的特定模式之间的句子。目的是从文件中提取出现在第一对“GO”字之间的句子。这里实现的逻辑是基于单词'GO'分割文件,然后打印数组的第二个元素(在本例中以SET开头的句子)。但是,PowerShell没有识别分隔符(GO);相反,它似乎认识到“新行”作为分隔符,并正在打印第二个句子。PowerShell拆分不能使用单词(从文件中读取)
请注意,我需要阅读文件,然后完成提取。
内容文件
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
很好解释,非常感谢。 -Raw参数的工作方式非常神奇!再次感谢。 –