Reputation: 99
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!
Upvotes: 5
Views: 16932
Reputation: 354586
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)
Upvotes: 7
Reputation: 60918
try
gc .\0.txt |
% { $_ -replace ' +',"`t" } |
set-content .\temp.txt
Upvotes: 3