当文件没有标题行时,如何使用Import-CSV导入制表符分隔的文本文件?

时间:2014-07-15 06:33:57

标签: powershell

我有一个巨大的文本文件(大约500 MB),以制表符分隔。它不包含标题。所以它看起来像:

20140711   IBM   29.9068    tom.smith@ibm.com    this is interesting
20140712   HP   2.3000    tom.smith@ibm.com    this is interesting
20140713   GOOGLE   44.9033    tom.smith@ibm.com    this is interesting
20140714   HTC   739.70    tom.smith@ibm.com    this is interesting
20140715   SAMSUNG   8.442    tom.smith@ibm.com    this is interesting
20140716   MICROSOFT   67.104    tom.smith@ibm.com    this is interesting
20140717   DELL   5.0823    tom.smith@ibm.com    this is interesting
...
...
...

我需要使用Powershell将文本作为表加载到SQL Server数据库中。由于文本文件中没有标题,因此" Import-Csv" cmdlet输出内容不正确。我认为它总是将第一行视为标题。

如何" Import-Csv"输出文本文件中的任何内容并忘记标题配置?

感谢。

3 个答案:

答案 0 :(得分:11)

你可以试试这个:

$data = Import-Csv C:\temp\F.csv -Header "date","company","value","mail","description" -Delimiter "`t"

答案 1 :(得分:1)

如果它是一个巨大的文件,我建议使用" -Raw'选项。

类似的东西:

$header= "Date   EventLog   EntryType"
(get-content -Raw $File) | foreach-object {$_ -replace "^", "$header`n"} | set-content  $File

亲切的问候

答案 2 :(得分:0)

$header3 = @("Column_1","Column_2","Column_3","Column_4","Column_5")        

$data = Import-Csv $filePath -Header $header3 -Delimiter "`t"