Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim all the lines in a file in powershell

I am able trim all the contents in file

$lines=(Get-Content $BaselineFile)

foreach ($line in $lines) { $line= $line.Trim() }

But how to put back the result in same file. ($Baselinefile)

like image 331
Samselvaprabu Avatar asked Jan 06 '12 10:01

Samselvaprabu


People also ask

How do you break a line in PowerShell?

Using PowerShell newline in Command To add newline in the PowerShell script command, use the` (backtick) character at the end of each line to break the command into multiple lines.

How do I remove leading zeros in PowerShell?

^0+ matches one or more ( + ) zeros at the start ( ^ ) of the string and replaces them with a single zero. Positive look-ahead assertion (? =[^.]) effectively removes all leading zeros ( ^0+ ) as long as such a zero isn't followed by a literal .

How do I skip the first line in PowerShell?

In PowerShell, using Get-Content to skip first line of file, use Skip parameter with value to skip line. It will skip those number of lines and display rest of the lines of file.

How do I write to a file in PowerShell?

Powershell Write Output to File. There are a couple of ways to write the output of PowerShell to a file. The most common ways are to use the Out-File cmdlet or the redirection operator > . Other options are to use the Set-Content and Add-Content cmdlet.


1 Answers

This is more concise:

(gc $BaseLineFile)| % {$_.trim()} | sc $BaseLineFile
like image 183
mjsr Avatar answered Nov 04 '22 07:11

mjsr