Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey PowerShell Guy !, How Can I Remove Extraneous Spaces From Fields in a Text File?

A little one before the, 2008 Winter Scripting Games

The PowerShell version of the Hey Scripting guy VBScript version  :  How Can I Remove Extraneous Spaces From Fields in a Text File?

(gc c:\scripts\test.txt) -replace ' {2,}',''

PoSH> gc c:\scripts\test.txt                                                                                            
Emailaddr                            |Firstname                        |Lastname                                        
"kmyer@fabrikam.com          "|"Ken                     "|"Myer            "|                                           
"aabudayah@fabrikam.com          "|"Ahmad          "|"Abu Dayah                "|                                       
"htandersen@fabrikam.com          "|"Henriette Thaulow              "|"Andersen"|                                       

PoSH> (gc c:\scripts\test.txt) -replace ' {2,}',''                                                                       
Emailaddr|Firstname|Lastname                                                                                            
"kmyer@fabrikam.com"|"Ken"|"Myer"|                                                                                     
"aabudayah@fabrikam.com"|"Ahmad"|"Abu Dayah"|                                                                           
"htandersen@fabrikam.com"|"Henriette Thaulow"|"Andersen"|                                                               

PoSH> (gc c:\scripts\test.txt) -replace ' {2,}','' | sc c:\scripts\test.txt                                              
PoSH> gc c:\scripts\test.txt                                                                                            
Emailaddr|Firstname|Lastname                                                                                            
"kmyer@fabrikam.com"|"Ken"|"Myer"|                                                                                     
"aabudayah@fabrikam.com"|"Ahmad"|"Abu Dayah"|                                                                           
"htandersen@fabrikam.com"|"Henriette Thaulow"|"Andersen"|                                                               
PoSH>                                                                                

Note 

  • GC is an alias for Get-Content
  • SC is an alias for Set-Content

And as we can see above,  if we want to save back the result :

(gc c:\scripts\test.txt) -replace ' {2,}','' | sc c:\scripts\test.txt

another one to easy to resist ;-)

Enjoy,

Greetings /\/\o\/\/

Published Monday, February 11, 2008 1:23 PM by MoW
Filed under: ,

Comments

# re: Hey PowerShell Guy !, How Can I Remove Extraneous Spaces From Fields in a Text File?

Thank you REGEX!

Monday, February 11, 2008 4:02 PM by twometertwo

# Interesting Finds: February 12, 2008

Tuesday, February 12, 2008 10:19 AM by Jason Haley

# re: Hey PowerShell Guy !, How Can I Remove Extraneous Spaces From Fields in a Text File?

A 16 line vbscript turned into a single line powershell script.  Just lovely

Wednesday, February 13, 2008 10:29 AM by AirJunkie

# re: Hey PowerShell Guy !, How Can I Remove Extraneous Spaces From Fields in a Text File?

Would this also work with an odd number of spaces padding the content?

And: Keep those one-liners coming...,

Tuesday, February 26, 2008 11:00 AM by plagwitz

# re: Hey PowerShell Guy !, How Can I Remove Extraneous Spaces From Fields in a Text File?

@ Plagwitz ,

Yes as long as it is not 1 , the Comma in the regex takes care of that see also original article

Greetings /\/\o\/\/

Tuesday, February 26, 2008 4:15 PM by MoW

# re: Hey PowerShell Guy !, How Can I Remove Extraneous Spaces From Fields in a Text File?

Hey PowerShell Guy!, How can I remove Carriage Returns from a text file?

This post helped with the extra spaces I have, but when I do a $address = Get-Content c:\workfile.txt it is putting the extra carriage returns into the variable as spaces.

If example needed, let me know. :)

Tuesday, May 13, 2008 2:22 PM by zolon

# how to remove spaces in a text file

Tuesday, June 10, 2008 12:34 PM by how to remove spaces in a text file
Anonymous comments are disabled