How to remove NULL char (0x00) from object within PowerShell

心已入冬 提交于 2021-02-07 06:47:35

问题


I have a .CSV file that I created using SQL Server's BCP command-line BULK-COPY utility to dump a bunch of database tables.

Since I want to import these .CSV file's using Powershell and convert them to a nice report using the format-table cmdlet, I'm having issues with columns lining up, etc,. because some columns contain NULLs from SQL Server. I don't have the option to convert the NULL from SQL Server first; due to the way I'm exporting the table to CSV.

Therefore, I would like to remove all NULLs from the .CSV file prior to trying to pipe it into the format-table cmdlet.

My basic code is below:

$CSV=import-csv "c:\temp\tablename.csv"
$CSV | format-table -autosize | out-string -width 4096 >"C:\TEMP\tablename.txt"

I've tried doing something like:

$CSV | -replace($null,"") | format-table -autosize | out-string -width 4096 > "C:\TEMP\tablename.txt"

but I'm still getting the NULLs.

Does anyone know how to remove the NULLs from my CSV so I can display a nice tabular report. I want to get these .TXT reports imported into SVN but the NULLs are going to cause me problems, plus it skews the reports.

CSV file as shown in a hex editor:

00000EA0h: 31 38 39 2C 31 31 39 2C 37 35 29 2C 77 68 69 74 189,119,75),whit  
00000EB0h: 65 2C 77 68 69 74 65 2C 2C 2C 2C 2C 2C 2C 2C 2C e,white,,,,,,,,,  
00000EC0h: 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C 2C ,,,,,,,,,,,,,,,,  
00000ED0h: 2C 2C 0D 0A 61 63 62 34 33 5F 30 31 2C 4F 4E 2C ,,..acb43_01,ON,  
00000EE0h: 00 2C 32 37 2C 39 39 2C 2F 61 63 62 34 33 5F 30 .,27,99,/acb43_0  
00000EF0h: 31 2F 34 33 62 61 6C 61 6E 63 65 73 2E 67 69 66 1/43balances.gif  

Notice at EE0h the first character is NULL, 0x00.


回答1:


After a bit of playing around, I finally figured out that this syntax worked:

(Get-Content "C:\temp\tablename.csv") -replace "`0", "" | Set-Content "C:\temp\tablename.csv"



回答2:


All of the submitted answers are work-arounds and do not address the core issue, which is that powershell uses the utf-16 encoding by default (this is why you're getting NULL i.e. 0x00 between all characters). The solution is to tell powershell to use utf-8:

$stuff | Out-File $out_path -Encoding UTF8

Also see this thread




回答3:


Update - Now that I see what you mean by NULL (hex 0x00) I can give you another approach.

You can just filter out these byte by reading the file as binary like this:

Get-Content "c:\temp\tablename.csv" -Encoding Byte | ? {$_ -ne 0x00} | Set-Content "c:\temp\tablename2.csv" -Encoding Byte



回答4:


Use '\xnn' to match characters by their hex representation:

(get-content c:\temp\tablename.csv) -replace '\x00','' | set-content c:\temp\tablename.csv



回答5:


-replace "`0", " "

This worked for me in a text file just fine.




回答6:


Going off Andy's reply and your response, it looks like the 'null' value you want to get rid of is actually "00" in text.

So you'd want to do this instead:

(Get-Content "C:\temp\tablename.csv") -replace " 00 ", " " | Set-Content "C:\temp\tablename.csv"

This will convert:

00000EE0h: 00 2C 32

into:

00000EE0h: 2C 32



来源:https://stackoverflow.com/questions/9863455/how-to-remove-null-char-0x00-from-object-within-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!