我想從文件中提取出現在單詞的特定模式之間的句子。目的是從文件中提取出現在第一對「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參數的工作方式非常神奇!再次感謝。 –