header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
insert ';' at end of row
Last Post 02 Jul 2010 10:41 AM by PoSherLife. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
tran008User is Offline
New Member
New Member
Posts:8
Avatar

--
02 Jul 2010 09:03 AM

    How can I insert a ';' to each row of the text file? The text file contain over 1 million row, and I need to have the ';' at the end of each row from the file.

    thanks

    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1643
    Avatar

    --
    02 Jul 2010 09:13 AM
    get-content input.txt|foreach{$_+";"}|out-file output.txt

    Now, be warned that PowerShell can play tricks with hidden characters in situations like this...
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    02 Jul 2010 09:17 AM
    To replace the content without creating a new file:

    (gc file.txt) | % { $_ + ";" } | sc file.txt
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    tran008User is Offline
    New Member
    New Member
    Posts:8
    Avatar

    --
    02 Jul 2010 10:06 AM
    thank you very much....
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    02 Jul 2010 10:41 AM
    i just noticed that the file has 1 million rows. Marco's solution will be the least resource intensive method. You could then use the following function to end with the same file name as you started with:

    function Add-TXT {
    param (
    [string]$file,
    [string]$temp = $env:TEMP + "\output.txt"
    )
    gc $file | % { $_ + ";" } | out-file $temp

    ri $file -Force

    $tempFldr = $temp.TrimEnd($temp.substring(($temp.LastIndexOf("\")),(($temp.Length - $temp.LastIndexOf("\") - 1))))
    $fileFldr = $file.TrimEnd($file.substring(($file.LastIndexOf("\")),(($file.Length - $file.LastIndexOf("\") - 1))))

    if ($tempFldr -eq $fileFldr) {
    rni $temp $file
    }
    else {
    mi $temp $file
    }
    }

    Then call it like such in your script:
    Add-TXT FileToCopy
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer