This is a Powershell question similar to what was being asked in this C# question: C# Question Link
I have fixed width column data in a text file that are of variable length, so I'd like to delimit the data with tabs. To do so, I want to use Powershell to read in the file, replace only multiple spaces with tabs using a Regex expression, keep the end of lines intact and output it to a temp file. I'll then rename it to the original.
I've searched the web and only seem to be able to find bits and pieces. Any assistance with this would be greatly appreciated!
Fetch the contents
$content = [IO.File]::ReadAllText('foo.txt')
Replace at least two spaces by a single tab:
$content = $content -replace ' {2,}', "`t"
Write back to a file
[IO.File]::WriteAllText('footab.txt', $contents)
try
gc .\0.txt |
% { $_ -replace ' +',"`t" } |
set-content .\temp.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With