powershell - Removing a Special Character from a Text file is returning Blank File -


i have text file , want remove special character file. example: sample@ in file tess.txt

i want remove special character text file , want output as:- sample

i have used below powershell script in spite of replacing special characters, deleting text file.

powershell "get-content c:\tess.txt | foreach-object { $_ -replace '@' } > c:\tess.txt" 

when try output file tess1.txt, see correct output sample.

powershell "get-content c:\tess.txt | foreach-object { $_ -replace '@' } > c:\tess1.txt" 

but did not want create new text file. want remove @ existing file.

i'm new powershell scripting. please help.

you cannot in pipeline without first reading entire file memory:

(get-content c:\tess.txt) | foreach-object { $_ -replace '@' } > c:\tess.txt 

this because get-content reading file 1 line @ time, , passing down pipeline, waiting pipeline finish line before asking next one. means you're trying overwrite file before you're finished reading it.

wrapping get-content expression in parentheses force reading of file contents before starting pipeline, file no longer being read time script tries overwrite it.


Popular posts from this blog