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.