【发布时间】:2019-01-18 17:47:12
【问题描述】:
我的备份软件生成了一个制表符分隔的文本文件。我希望能够使用 Import-CSV 将其作为 PowerShell 数组导入。
问题是导出文件的前 5 行阻止 PowerShell 将其识别为数组:
#List of Media
#Cell Manager: hpdp.domain.local
#Creation Date: 11/08/2018 9:36:09 AM
# Headers
# Medium ID Label Location Status Protection Used [MB] Total [MB] Last Used Last Used_t Pool Type
我能够“解决”这个问题的方法是使用记事本删除前四行并从标题行中删除第一个 #。然后文件如下所示:
Medium ID Label Location Status Protection Used [MB] Total [MB] Last Used Last Used_t Pool Type
id1:id2:id3:id5 [hostname] labelname_31 [Library: 5] Good 13/08/2018 10:01:41 PM 762699.50 762699.50 14/07/2018 10:00:36 PM 1531828836 Tape Drive Pool LTO-Ultrium
我正在尝试找到一种方法让 PowerShell 进行此更改。例如,我尝试使用以下行作为第一步删除前四行:
(Get-Content $file | Select-Object -Skip 4) | Set-Content $file
该文件似乎随后失去了制表符,因此不再分隔。
如何在不丢失标签的情况下删除行和第一个 #?提前致谢!
【问题讨论】:
-
如果您的数据中没有
#,那么您可以使用(Get-Content $file | Select-Object -Skip 4) -replace ("#", "") | Set-Content $file -
谢谢,我试过了,但它仍然会丢失标签。事实上,只要执行“(Get-Content $file) | Set-Content $file”就会丢失选项卡。
标签: powershell text tabs import-csv