Sorting data from .txt

Welcome Forums General PowerShell Q&A Sorting data from .txt

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
4 years, 9 months ago.

  • Author
    Posts
  • #18914

    Participant
    Topics: 11
    Replies: 13
    Points: 0
    Rank: Member

    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

    Participant
    Topics: 0
    Replies: 12
    Points: 3
    Rank: Member

    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(' ',',')}
    

The topic ‘Sorting data from .txt’ is closed to new replies.