AJennings1
AJennings1

Reputation: 159

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

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.

Upvotes: 3

Views: 27856

Answers (3)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

There a several ways to approach a task like that. One is to use a regular expression replacement with a negative lookahead assertion:

(Get-Content 'C:\path\to\input.txt' | Out-String) -replace "`r?`n(?!`r?`n)" |
    Set-Content 'C:\path\to\output.txt'

You could also work with a StreamReader and StreamWriter:

$reader = New-Object IO.StreamReader 'C:\path\to\input.txt'
$writer = New-Object IO.StreamWriter 'C:\path\to\output.txt'

while ($reader.Peek() -gt 0) {
    $line = $reader.ReadLine()
    if ($line.Trim() -ne '') {
        $writer.Write($line)
    } else {
        $writer.WriteLine()
    }
}

Upvotes: 0

TechSpud
TechSpud

Reputation: 3518

# 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', ''

Upvotes: 7

Simon Catlin
Simon Catlin

Reputation: 2229

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.

Upvotes: 2

Related Questions