Search code examples
powershellposition

How to change column position in powershell?


Is there any easy way how to change column position? I'm looking for a way how to move column 1 from the beginning to the and of each row and also I would like to add zero column as a second last column. Please see txt file example below.
Thank you for any suggestions.

File sample

TEXT1,02/10/2015,55.930,57.005,55.600,56.890,1890  
TEXT2,02/10/2015,51.060,52.620,50.850,52.510,4935
TEXT3,02/10/2015,50.014,50.74,55.55,52.55,5551

Output:

02/10/2015,55.930,57.005,55.600,56.890,1890,0,TEXT1  
02/10/2015,51.060,52.620,50.850,52.510,4935,0,TEXT2
02/10/2015,50.014,50.74,55.55,52.55,5551,0,TEXT3

Solution

  • Another option:

    #Prepare test file
    (@'
    TEXT1,02/10/2015,55.930,57.005,55.600,56.890,1890  
    TEXT2,02/10/2015,51.060,52.620,50.850,52.510,4935
    TEXT3,02/10/2015,50.014,50.74,55.55,52.55,5551
    '@).split("`n") |
    foreach {$_.trim()} | 
    sc testfile.txt
    
    #Script starts here
    $file = 'testfile.txt'
    
    (get-content $file -ReadCount 0) |
    foreach  {
    '{1},{2},{3},{4},{5},{6},0,{0}' -f $_.split(',')
    } | Set-Content $file
    
    #End of script
    
    #show results    
    get-content $file
    
    02/10/2015,55.930,57.005,55.600,56.890,1890,0,TEXT1
    02/10/2015,51.060,52.620,50.850,52.510,4935,0,TEXT2
    02/10/2015,50.014,50.74,55.55,52.55,5551,0,TEXT3