0

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.

  • 2
    `$SplitData = Get-Content -Raw file.txt) -split '(?s)({-.+?-})\r?\n' -ne ''`? – iRon Jan 30 '23 at 08:51
  • Kindly edit the question and give sample data or files to work with. I dont know why you have added the entire regular expression to split for your data. – Ranadip Dutta Jan 30 '23 at 09:20
  • 1
    It's implied by @iRon's comment, but to spell it out: `(Get-Content -Raw foo.data) -split '(?s)({-.+?-})\r?\n' -ne ''` directly outputs an _array_ of line blocks, and you can assign that array to a variable. The index in the original solution is solely used to sequentially name the output files. – mklement0 Jan 30 '23 at 22:16

0 Answers0