I have a text file which I import using Get-Content
It looks like this:
aago go sdf
bbbgo go sdf gojh
go
zzzz bbb
go
sdkfgo go sdfd
go
I want to -split
it based on 'go' which is single liner "go", aka i want 3 array items only. So -split 'go'
won't work.
How to specify delimiter for split where the only item in line in 'go', OR where after 'go' there is a new line?
So output I expect is this
$output[0]=
"aago go sdf
bbbgo go sdf gojh
"
$output[1]=
"zzzz bbb
"
$output[2]=
"sdkfgo go sdfd
"
CodePudding user response:
# Sample input, defined via a verbatim here-string.
$str = @'
aago go sdf
bbbgo go sdf gojh
go
zzzz bbb
go
sdkfgo go sdfd
go
'@
$str -split '(?m)^go\s*$' -ne '' | # Split as desired.
ForEach-Object { "«$_»" } # Visualize the results
For an explanation of the regex used with the -split
operator above and the ability to experiment with it, see this regex101.com page.
Output:
«aago go sdf
bbbgo go sdf gojh
»
«
zzzz bbb
»
«
sdkfgo go sdfd
»