2

My quest is to take the contents of a text file similar to below ...

A typical line
A typical line
A line that is 5,000 characters long ...............
A typical line
A line that is 30,000 characters long ......................

And split the extremely long lines at $x characters (probably 2056) so it looks like...

A typical line
A typical line
A line that is 2056 characters long (max)
A line that is 2056 characters long (max)
A line that is 2056 characters long (max)
A typical line
A line that is 2056 characters long (max)
The rest of that 30,000 char line ... etc.

I don't know what I'm doing, this was my best guess :

$originalfile = "C:\test\file.txt"
$output = "C:\test\output.txt"

foreach($line in Get-Content $originalfile){
    if ($line.length -gt 2056){
        $line -split ... ???
    } else {
        $line | out-file -Append $output
    }
}

I tried this example I found :

(Get-Content $originalfile) -join " " -split '(.{2056,}?[ |$])' | Where-Object{$_} | out-file $output

... but I could never get the output to work, it just put it in one long string but it did split them at the 2056.

A typical line A typical line A line that is 5,000
characters long ............ A Typical line A line that
is 30,000 characters long.

In a perfect world I would try to split on a space, but after two days of google searching I've basically given up and don't care if it splits words in half.

jalexander
  • 35
  • 6
  • So you're trying to implement word wrapping? – Maximilian Burszley Jul 02 '18 at 18:32
  • Possible duplicate of [Is there a way to wordwrap results of a Powershell cmdlet?](https://stackoverflow.com/questions/1059663/is-there-a-way-to-wordwrap-results-of-a-powershell-cmdlet) –  Jul 02 '18 at 18:38
  • Not a dupe of that for sure. The user is trying to wrap the contents of a text file. Not cmdlet output. I suppose that you could take some concepts from the answers to get a workable answer for this question but the question itself is not a dupe.\ – EBGreen Jul 02 '18 at 19:09
  • A text file may contains a multibyte unicode chars (UTF-8, for example). It's possible that some multibyte char is placed on 2056 and 2057 bytes. It should not break the char. I think it need to modify your quest. – mazzy Jul 03 '18 at 05:06

2 Answers2

2

Get the console width and add a line break every width characters (this doesn't take spaces into account):

# Really long string from whatever command
$mySuperLongOutputString = "SOMETHING REALLY LONG, LONGER THAN THIS"

# Get the current console width
$consoleWidth = $Host.UI.RawUI.WindowSize.Width

# For loop to iterate over each intended line in the string
for( $i = $consoleWidth; $i -lt $mySuperLongOutputString.Length; $i += $consoleWidth ) {
  # Insert string at the end of the console output
  $mySuperLongOutputString = $mySuperLongOutputString.Insert( $i, "`r`n" )

  # Bump the counter by two to skip counting the additional newline characters
  $i += 2
}

The console width is equal to the number of columns wide your buffer is.

codewario
  • 19,553
  • 20
  • 90
  • 159
1

I did end up getting this to work (mostly). It does split a little of the first word in a line, but I probably just need to tweak the regex a little bit more.

foreach($line in Get-Content $originalfile){
    if ($line.length -gt 2056){
        $linearray = [regex]::split($line, '(.{2000}\s)') 
        for ($i=0; $i -lt $linearray.length; $i++) {
            $linearray[$i] | out-file -Append $output
        }
        $linearray=@()
    } else {
        $line | out-file -Append $output
    }
}

Apologies for not explaining this problem very well to begin with, my brain is just not geared for this sort of stuff. Thank you Bender for your answer, although I could not get it to work. I'm guessing because the text file is in an array (the .insert would not work for me), but it did get me to research in a different direction.

jalexander
  • 35
  • 6
  • 1
    My answer does assume your text is in a single string, not an array. This can be achieved usig `Get-Content` though with `$text = Get-Content $filename | Out-String`. But I'm glad my answer set you down a path to find your own solution that fits your scenario, which IMO is better than simply using an answer by another user :) – codewario Jul 06 '18 at 15:50