powershell v3.0 - Remove the last pipe from a text file -


objective: need remove last | file, if last \w character in file.

why following syntax append instead of replacing?

[io.file]::readalltext(".\example.txt") -replace '`|$','' > .\example.txt 

i tried

[io.file]::readalltext(".\example.txt") -replace '\|$','' > .\example.txt 

...which doesn't anything, seems. not surprisingly, equivalent get-content doesn't work either:

(get-content .\example.txt) | foreach-object {$_ -replace '$\|', ''} | set-content .\example.txt 

i assume issue parsing pipe, uncertain how compensate it.

thanks in #powershell on freenode, have now.

$content = get-content example.txt; if ($content[-1] -eq '|') {     $content.substring(0,$content.length-1) > example.txt } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -