Multi-Line String to Single-Line String conversion in PowerShell

17,809

Solution 1

# create a temp file that looks like your content
# add the A,B,C,etc to each line so we can see them being joined later
"Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Gxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Ixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | Set-Content -Path "$($env:TEMP)\JoinChunks.txt"

# read the file content as one big chunk of text (rather than an array of lines
$textChunk = Get-Content -Path "$($env:TEMP)\JoinChunks.txt" -Raw

# split the text into an array of lines
# the regex "(\r*\n){2,}" means 'split the whole text into an array where there are two or more linefeeds
$chunksToJoin = $textChunk -split "(\r*\n){2,}"

# remove linefeeds for each section and output the contents
$chunksToJoin -replace '\r*\n', ''

# one line equivalent of above
((Get-Content -Path "$($env:TEMP)\JoinChunks.txt" -Raw) -split "(\r*\n){2,}") -replace '\r*\n', ''

Solution 2

A bit of a fudge:

[String]   $strText  = [System.IO.File]::ReadAllText(  "c:\temp\test.txt" );
[String[]] $arrLines = ($strText -split "`r`n`r`n").replace("`r`n", "" );

This relies on the file having Windows CRLFs.

Share:
17,809
AJennings1
Author by

AJennings1

Updated on June 04, 2022

Comments

  • AJennings1
    AJennings1 almost 2 years

    I have a text file that has multiple 'chunks' of text. These chunks have multiple lines and are separated with a blank line, e.g.:

    This is an example line
    This is an example line
    This is an example line
    
    This is another example line
    This is another example line
    This is another example line

    I need these chunks to be in single-line format e.g.

    This is an example lineThis is an example lineThis is an example line
    
    This is another example lineThis is another example lineThis is another example line

    I have researched this thoroughly and have only found ways of making whole text files single-line. I need a way (preferably in a loop) of making an array of string chunks single-line. Is there any way of achieving this?

    EDIT: I have edited the example content to make it a little clearer.