Hi All. I want to use get-content and -replace to replace empty spaces:
The text file
Line1:abcd
Line2:cdef
Line3:jukq
Line4:casdj
Technology Tips and News
Hi All. I want to use get-content and -replace to replace empty spaces:
The text file
Line1:abcd
Line2:cdef
Line3:jukq
Line4:casdj
First of all, this isn't relevant to DPM so would have been more appropriate being posted in http://social.technet.microsoft.com/Forums/windowsserver/en-US/home?forum=winserverpowershell and potentially have a better chance of being replied to before now.
That said, doing what you're after is fairly straight forward, though the exact method depends on what you're trying to achieve.
If you want to update the existing file c:\input.txt so that same file ends up with the spaces removed, then use :
(Get-Content C:\input.txt) | Foreach-Object {$_ -replace " ",""} | Set-Content C:\input.txtAlternatively, if you're trying to load those values into a variable ($foo) but with the spaces removed then you can use :
$foo = (Get-Content C:\input.txt) | Foreach-Object {$_ -replace " ",""}
You can use the Trim() method as in:
Set-Content -Path .\spaces.txt -Value (Get-Content -Path .\spaces.txt).Trim() -Force
using {$_ -replace " ",""} should only take out 1 empty character...
the point is I want to replace the space at the beginning of each line
Sorry if I'm being stupid but that's what the code I provided does. If input.txt contains
Line1:abcd
Line2:cdef
Line3:jukq
Line4:casdj
then the output of that code will be :
Line1:abcd
Line2:cdef
Line3:jukq
Line4:casdj
with the spaces at the beginning removed, which is what I thought you were after.