none
powershell text to csv RRS feed

  • Question

  • Greeting,

    need help for convert the text file to CSV file.

    File as below:

    VM                Name                      Description        Size       

    --                  ----                          ----------             ----

    PRODAD1     W2012                    Lab AD              80.0 GB

    Workstation W10                                                20.00 GB

    Server          Windows 2008       Test server       200.00 GB

    points are here:

    1. I have titles, between are Spaces

    2. All the fields length could be long or  small, with space as well

    3. Each line have the same length.

    4. all the gaps in line was filled by spaces.

    Thanks and Best Regards,

    newbie in Powershell

    Monday, June 26, 2017 3:21 PM

Answers

  • What application? Tell it to produce CSV output instead of space-formatted output. Then you can simply use Import-Csv. This is by far the fastest way to solve your problem.


    -- Bill Stewart [Bill_Stewart]

    Monday, June 26, 2017 8:32 PM
    Moderator

All replies

  • import-csv tabdelimitedfile.txt -delimiter "`t" | export-csv csvfile.csv

    Note: A tab separated text file could be treated as a CSV file with a tab instead of comma as its delimiter. 

    Monday, June 26, 2017 3:28 PM
  • thanks for reply, But I do not have ANY tab in the file

    newbie

    Monday, June 26, 2017 3:34 PM
  • Count the field widths and use "SubString" to extract the columns.

    \_(ツ)_/

    Monday, June 26, 2017 6:17 PM
  • Where did the input data come from?

    -- Bill Stewart [Bill_Stewart]

    Monday, June 26, 2017 6:17 PM
    Moderator
  • thanks for advise, but the thing is next time the column width maybe change

    thanks

    newbie

    • Edited by userausera Monday, June 26, 2017 7:50 PM
    Monday, June 26, 2017 7:49 PM
  • was from an application

    thanks

    newbie

    Monday, June 26, 2017 7:49 PM
  • thanks for advise, but the thing is next time the column width maybe change

    thanks

    newbie

    That is doubtful.  Have you tried.  We cannot run your system for you and we cannot guess at what you have in your file.


    \_(ツ)_/

    Monday, June 26, 2017 7:56 PM
  • What application? Tell it to produce CSV output instead of space-formatted output. Then you can simply use Import-Csv. This is by far the fastest way to solve your problem.


    -- Bill Stewart [Bill_Stewart]

    Monday, June 26, 2017 8:32 PM
    Moderator
  • Thanks Bill,

    Import-csv works now,

    Previous I was try to add "FT -AutoSize", and that was mark import-csv or export-csv got strange chars

    thanks

    NewBie in Powershell

    Tuesday, June 27, 2017 3:58 PM
  • tried substring, make my code very hard to understand, any way it is a good idea as well.

    thanks

    NewBie in Powershell

    Tuesday, June 27, 2017 3:59 PM
  • Correct. Don't use Format-Table if you are producing output that is going to be consumed by another program.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 27, 2017 9:37 PM
    Moderator