How to import a tab delimited text file using Import-CSV when the file does not have a header line?

I have a huge text file (about 500 MB) with a tab delimiter. It does not contain headers. It looks like this:

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
...
...
...

I need to use Powershell to load text into a SQL Server database as a table. Since there are no headers in the text file, the Import-Csv cmdlet does not display the contents correctly. I think it always treats the first line as headers.

How does "Import-Csv" display the entire contents of a text file and forget about the header configuration?

Thank.

+4
source share
3 answers

You can try the following:

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

, "-Raw".

- :

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

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

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

Source: https://habr.com/ru/post/1548440/


All Articles