none
Powershell exporting CSV RRS feed

  • Question

  • Hello.

    I'm attempting to work with Powershell to take the contents of a large csv file and split up the file 12,000 lines at a time. The script below is getting me close to what I need but when I try to export to csv, the output is nothing along the lines of the original data in the original csv file. The output is along the lines of information relative to the file and not the data within the file. I'd like to keep the coding relatively simplistic if possible.

    Thanks

    script:

    (get-content -delimiter ',' myfile.csv)[0..12000] | export-csv test.csv

    Friday, May 6, 2016 6:11 PM

Answers

  • get-content C:\windows\setupact.log -ReadCount 12000 | %{$i++;$_ | Out-File "NewFile$i.Csv"}

    Breaks up a file into 12000 line files.


    \_(ツ)_/

    • Marked as answer by bigtj1 Friday, May 6, 2016 10:20 PM
    Friday, May 6, 2016 9:12 PM
    Moderator

All replies

  • First off, use Import-Csv, not Get-Content. Second, 0..12000 is 12,001 rows. As it's zero-based, you'd choose 0..11,999.
    Friday, May 6, 2016 6:23 PM
  • (Import-CSV -delimiter ',' myfile.csv)[0..12000] | export-csv test.csv -NoTypeInformation
    Friday, May 6, 2016 6:32 PM
  • Thank you both for this information. I had been trying to work with the Import-CSV cmdlet but when I run it in that specific way mentioned above, I receive the following error:

    Import-Csv : Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" argument and run the operation again.
    Friday, May 6, 2016 6:51 PM
  • So I believe some of my problem is due to the fact that I'm running Powershell version 2. So on another machine I upgraded to version 4. I have a mostly working script now but have the issue of wanting to not have any header pulled or created from the parent or child csv file. Here is my script below:

    (Import-CSV myfile.csv)[0..12000] | export-csv test.csv -NoTypeInformation

    Friday, May 6, 2016 7:37 PM
  • Clearly the week is catching up with me. I now realize I'll need a bit more robust script to prevent myself from having to manually script out each block of 12,000 lines from my parent csv file.

    Might there be a way to parse my parent csv file and export every line to a unique csv in blocks of 12,000 at a time?

    Thanks!

    Friday, May 6, 2016 8:59 PM
  • get-content C:\windows\setupact.log -ReadCount 12000 | %{$i++;$_ | Out-File "NewFile$i.Csv"}

    Breaks up a file into 12000 line files.


    \_(ツ)_/

    • Marked as answer by bigtj1 Friday, May 6, 2016 10:20 PM
    Friday, May 6, 2016 9:12 PM
    Moderator
  • This is great! It is incredibly close to what I'm looking for. The caveat I'm having with this script is for the script to read the parent csv then export lines in blocks of 12,000 in the same tabular format. I'll play around with some modifications of this script but thought I would post the question now to see if changing the "get-content" portion to "import-csv" and/or changing the "out-file" to "export-csv" would give me the output I'm looking for?

    Thanks again thus far!

    Friday, May 6, 2016 10:02 PM
  • There is no tabular format.  CSV is a simple freeform text file.  The line can be of any length.  They are not tabular. Each field is delimited by commas. A field can be empty (,,)


    \_(ツ)_/

    Friday, May 6, 2016 10:08 PM
    Moderator
  • Ok, thanks. I was over-thinking, or rather under-thinking, on the output of the data from this script. I was opening my parent file in excel and then opening the files created from this script in excel as well. The child files were not formatting themselves within the columns properly but when opening the csv child files in just a plain text editor, their format still matches the parent file.

    Thank you all very much for helping me out with working on this script!

    Friday, May 6, 2016 10:20 PM
  • The files, all but the first, lack a header. You asked for that. Without the header the file will not display headers in Excel.


    \_(ツ)_/

    Friday, May 6, 2016 11:07 PM
    Moderator
  • That's right, dropping the headers loses the column association, in a sense. Thanks again, I was beginning to go cross-eyed playing around with different cmdlets and such.
    Friday, May 6, 2016 11:10 PM