我是Powershell的新手,我正在尝试删除文件中的NUL字符。以下是文件快照
原始文件快照
我尝试使用下面的代码删除第2行中的NUL字符
$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
但是代码将所有记录都变成了一条记录。
我也尝试过下面的代码
$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
}
但是删除了NUL字符却使第二行看起来像多行
转换后的文件图像
我的要求是删除NUL字符,并将第二行作为单行而不是多行。对此表示任何帮助
这对我有用。 b前面有一个空值。 0d 0a是回车,换行。
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..