2

Im a newbie to powershell, I am trying to remove NUL character in the file. Below is snapshot of file

Original file snaphsot

Original file snaphsot

I tried using below code to remove NUL character in line2

$filepath=<File name>
[STRING]$test = [io.file]::ReadAllText($filepath)
$test = $test -replace "\x00","`n"
$test = $test -replace "`n"," " 
echo $test
$test | out-file ./testoutput.txt

but the code made all record into single record.

I also tried below code

$filepath=<filename>
$tempath=<temp file name>
Move-Item -Path $filepath -Destination $temppath
ForEach ($file in (Get-Content $temppath)) {
[STRING]$test = $file
$test = $test -replace "`0",""
$test = $test -replace "`r`n","" 
echo $test
$test | out-file $filepath -Append
}

that removed NUL character however second row which made to appear like multiple row

Converted file image

Converted file image

My requirement is to remove NUL character and make second row as single row instead multiple row .appreciate any help on this

Mustafa
  • 977
  • 3
  • 12
  • 25
Sandy
  • 21
  • 2
  • [1] what version of PoSh are you using? remove all the ones that you are NOT using from the tags. [2] please add code formatting for your code. the how-to for that is linked on the page yo used to create your Question. – Lee_Dailey Mar 21 '20 at 11:41

2 Answers2

2

It is difficult to tell what you are trying to accomplish. It appears that you have NUL characters potentially throughout your file.

If you want to remove all NUL characters, you can do the following:

(Get-Content $filepath) -replace '\0' | Set-Content $temppath

If you want to remove lines that contain only NULCRLF, you can do the following:

(Get-Content $filepath) -notmatch '^\0$' | Set-Content $temppath

If you want to remove all NUL characters and lines that contain only CRLF or NULCRLF, you can do the following:

(Get-Content $filepath) -replace '\0' -ne '' | Set-Content $temppath

If you want to remove all NUL characters and join lines that do not end with |, you can do the following:

$output = [System.Text.StringBuilder]::new()
(Get-Content $filepath) -replace "\0" | Foreach-Object {
    if ($_ -match '\|$') {
        [void]$output.AppendLine($_)
    } else {
        [void]$output.Append($_)
    }
}
$output.ToString() -replace '\r\n$' | Set-Content $temppath
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
0

This works for me. There's a null right before the b (62). 0d 0a is carriage return, linefeed, or "`r`n". Windows will display nulls as spaces.

echo a "`0b" c | set-content file
format-hex file


           Path: C:\Users\js\foo\file

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   61 0D 0A 00 62 0D 0A 63 0D 0A                    a...b..c..


(get-content file) -replace "`0" | set-content file2
format-hex file2


           Path: C:\Users\js\foo\file2

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   61 0D 0A 62 0D 0A 63 0D 0A                       a..b..c..

js2010
  • 23,033
  • 6
  • 64
  • 66