如何从PowerShell中的对象中删除NULL char(0x00) [英] How to remove NULL char (0x00) from object within PowerShell

查看:111
本文介绍了如何从PowerShell中的对象中删除NULL char(0x00)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.CSV文件,该文件是使用SQL Server的BCP命令行BULK-COPY实用程序创建的,用于转储一堆数据库表.

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.

由于我想使用Powershell导入这些.CSV文件,并使用format-table cmdlet将它们转换为漂亮的报告,因此我遇到了列对齐等问题.因为某些列包含SQL Server中的NULL.我没有选择先从SQL Server转换NULL的选项;由于我将表格导出为CSV的方式.

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.

因此,我想先将.CSV文件中的所有NULL删除,然后再尝试将其输送到format-table cmdlet中.

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

我的基本代码如下:

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

我尝试做类似的事情:

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

但是我仍然得到NULL.

but I'm still getting the NULLs.

有人知道如何从CSV中删除NULL以便我可以显示一个漂亮的表格报告.我想将这些.TXT报告导入到SVN中,但是NULL会给我带来问题,而且它会使报告倾斜.

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文件,如十六进制编辑器所示:

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  

在EE0h处注意,第一个字符为NULL,0x00.

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

推荐答案

经过一番摸索,我终于发现此语法有效:

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"

这篇关于如何从PowerShell中的对象中删除NULL char(0x00)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆