Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import a tab delimited text file with Import-CSV when the file has no header row?

Tags:

powershell

I have a huge text file (around 500 MB) that is tab delimited. It doesn't contain headers. So it looks like:

20140711   IBM   29.9068    [email protected]    this is interesting
20140712   HP   2.3000    [email protected]    this is interesting
20140713   GOOGLE   44.9033    [email protected]    this is interesting
20140714   HTC   739.70    [email protected]    this is interesting
20140715   SAMSUNG   8.442    [email protected]    this is interesting
20140716   MICROSOFT   67.104    [email protected]    this is interesting
20140717   DELL   5.0823    [email protected]    this is interesting
...
...
...

I need to use Powershell to load the text into the SQL Server database as a table. As there is no headers in the text file, the "Import-Csv" cmdlet output the content incorrectly. I think it always treats the first line as headers.

How can "Import-Csv" output whatever in the text file and forget about header config?

Thanks.

like image 316
alextc Avatar asked Jul 15 '14 06:07

alextc


People also ask

How do I read a CSV file in R without header?

By default, the functions read the header of the files. In case you want to read the CSV without header you will need to set to FALSE the header argument.

How do I convert a tab separated text to CSV?

Go to the File menu, choose 'Open CSV\Tab-Delimited File' (or simply press Ctrl+O), and then from the open dialog-box, choose the tab-delimited file to open. You can copy the tab-delimited string to the clipboard and then use the 'Open Text In Clipboard' option (Ctrl+F7).


3 Answers

You can try this :

$data = Import-Csv C:\temp\F.csv -Header "date","company","value","mail","description" -Delimiter "`t"
like image 146
JPBlanc Avatar answered Sep 30 '22 05:09

JPBlanc


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

$data = Import-Csv $filePath -Header $header3 -Delimiter "`t"
like image 31
user3100105 Avatar answered Sep 30 '22 06:09

user3100105


If it's a huge file, may I suggest to use "-Raw' option.

Something like:

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

Kind regards

like image 29
Alexis-Emmanuel Haeringer Avatar answered Sep 30 '22 05:09

Alexis-Emmanuel Haeringer