Sorting data from .txt

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of adi dumitras adi dumitras 2 years, 2 months ago.

  • Author
    Posts
  • #18914
    Profile photo of lemonade
    lemonade
    Participant

    I need to be able to take the data below and get into a db or .csv. Issue is the data isn't delimited at all. Its just line after line of 166 characters (17 columns without headings).

    Data example
    01/30/14 09:56:23 Data Received: 300114 0956 00824 9 12345 111 111 111 0 0 12345
    01/30/14 09:56:23 Data Received: 300114 0956 00553 9 12345 111 111 111 0 0 12345
    01/30/14 09:56:23 Data Received: 300114 0956 00136 7 1 111 01234567890# 12345 111 0 0 12345
    01/30/14 09:56:23 Data Received: 300114 0956 00016 9 12345 1234567890 111 111 0 0 12345 1234567890

    Below replaces the whitespace with commas, but all on one line. Is it possible to do this so the lines stay in tact inputting commas?

    ((Get-Content "$Env:USERPROFILE\Desktop\test.txt") -split '\s+' | Foreach-object { "$_" } ) -join ',' | out-file "$Env:USERPROFILE\Desktop\test2.txt"
    

    Thanks

  • #18916
    Profile photo of adi dumitras
    adi dumitras
    Participant

    Hi. I made some modifications to your command. The problem is that you were were splitting the whole file by Space and not doing it line by line. Now it should be OK:

    get-content "$Env:USERPROFILE\Desktop\test.txt" | ForEach-Object { add-content -path "$Env:USERPROFILE\Desktop\test2.txt" -value $_.replace(' ',',')}
    

You must be logged in to reply to this topic.