Building on an solution for breaking a file into blocks, how would those blocks rather be added to an array instead of written to a series of files?
Here, there's a single line of useful information between the delimiters. Assuming multiple lines, how would that be handled?
nicholas@mordor:~/powershell$
nicholas@mordor:~/powershell$ cat foo.data
{-
MT103 payment 1
-}
{-
MT103 payment 2
-}
nicholas@mordor:~/powershell$
nicholas@mordor:~/powershell$ cat split.ps1
$dataBlock = @()
$index = 1
# Split the file into blocks and write them to "outFile<index>.txt" files.
(Get-Content -Raw foo.data) -split '(?s)({-.+?-})\r?\n' -ne '' |
# add current block to the array
# increment the array for each split (?)
}
nicholas@mordor:~/powershell$
Each time a block is written to a file the $index
is incremented how, exactly? the original code is to:
(Get-Content -Raw file.txt) -split '(?s)({-.+?-})\r?\n' -ne '' |
Set-Content -LiteralPath { 'outFile{0}.txt' -f $script:index++ }
so there's an index++
, but rather than set content can the index of an array simply be set to __________?
The code is a bit indecipherable but quite intriguing.
From the console:
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $data = Get-Content -Raw foo.data
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $data
{-
MT103 payment 1
-}
{-
MT103 payment 2
-}
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $splitData = [string[]]$Array = $data.Split("{")
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $splitData
-
MT103 payment 1
-}
-
MT103 payment 2
-}
PS /home/nicholas/powershell>
but from $splitData
still not quite seeing how to write each block to an array of blocks. Assuming more complex input with multiple lines per block.
See also a tool for transposing each block.