5

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!

2 Answers 2

7
  1. Fetch the contents

    $content = [IO.File]::ReadAllText('foo.txt')
    
  2. Replace at least two spaces by a single tab:

    $content = $content -replace ' {2,}', "`t"
    
  3. Write back to a file

    [IO.File]::WriteAllText('footab.txt', $contents)
    
Sign up to request clarification or add additional context in comments.

8 Comments

First, thank you! That was wicked fast! Also, what does the $utf8 statement add to this and is it necessary on a plain ole ASCII file? Thank you again!
It just specifies that the output should be written as UTF-8 without a BOM. You can omit it if it's just ASCII, I think. In fact, without the encoding parameter the method does exactly that, so it's unnecessary in either case.
Awesome! Now I'm going to incorporate this into a VBA shell call, so I don't have to leave my Excel macro :) Doing this in VBA would be many more lines of code! Thanks again!
I thought it was working, but the footab file is still empty. Any ideas?
Try a complete path to the input and output file.
|
3

try

gc .\0.txt  | 
 % { $_ -replace '  +',"`t" } |   
     set-content .\temp.txt

8 Comments

@AnsgarWiechers Edited with 2 ' ' space char ;)
Are you saying that single spaces would be replaced? If so, that is not what I need. The data have single spaces in them, each column always has 2 or more spaces between them. Thank you!
@LawrenceKnowlton NO, single spaces aren't replaced.
Cool! I'll try to decipher what you have there. I know you are using the alias for GetContent, but after that the syntax is ehm taxing lol! Thanks!
@LawrenceKnowlton + in regular expressions means "the previous expression one or more times". In this case the previous "expression" is just a single space, and this expression is preceeded by another space. Thus the whole expression translates to "a space followed by one or more spaces" (or "2 or more spaces" for short).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.