Windows Vista Forums

split string every tot chars
  1. #1


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 03:25 AM
    italy

    split string every tot chars

    Hi guys. Is it possibile to split a string every tot chars?
    Let's say I have a string like this



    aabbcc

    is it possible to have a text file like
    aa
    bb
    cc

    ?

    Thanks in advance.

      My System SpecsSystem Spec

  2. #2


    Jon Guest

    Re: split string every tot chars

    "sardinian_guy" <guest@xxxxxx-email.com> wrote in message
    news:99bbd9d630d7367014be2b143c9b2b85@xxxxxx-gateway.com...

    >
    > Hi guys. Is it possibile to split a string every tot chars?
    > Let's say I have a string like this
    >
    > aabbcc
    >
    > is it possible to have a text file like
    > aa
    > bb
    > cc
    >
    > ?
    >
    > Thanks in advance.
    >
    >
    > --
    > sardinian_guy




    It's not particularly pretty, but perhaps something like ....

    $a = "aabbcc"
    for ($i = 0; $i -le ($a.length-1); $i += 2) {"$($a[$i])$($a[$i+1])"}

    --
    Jon




      My System SpecsSystem Spec

  3. #3


    Kiron Guest

    Re: split string every tot chars

    Also:
    $str = 'aabbcc'

    # v1
    [regex]::split($str, '(.{2})') | ? {$_}

    # v2
    $str -split '(.{2})' | ? {$_}

    --
    Kiron

      My System SpecsSystem Spec

  4. #4


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 03:25 AM
    italy

      Thread Starter

    Re: split string every tot chars

    Hi Jon. Thanks for your reply. It works perfectly but i have to split a string of thousands chars.
    Now I try to explain exactly my problem.

    I have a file like this
    string1of16chars
    string2of16chars
    string3of16chars
    string4of16chars
    string5of16chars
    string6of16chars

    and so on.

    I have to join these lines grouping them three by three.
    So the first line become

    string1of16charsstring2of16charsstring3of16chars

    the second
    string4of16charsstring5of16charsstring6of16chars

    and so on

    I don't know how to solve this problem. Using add-content I've created a kilometric one-line string and now I was looking for a way to split every 48 chars but maybe with a loop it's possible to join the lines three by three.

    Thanks again.

      My System SpecsSystem Spec

  5. #5


    Jon Guest

    Re: split string every tot chars

    A slight variation of Kiron's earlier suggestion would probably work better
    than my looping construct eg

    $str = "<your superlong combined string>"
    [regex]::split($str, '(.{48})') | ? {$_}

    --
    Jon


    "sardinian_guy" <guest@xxxxxx-email.com> wrote in message
    news:35a5127870792c67124cea2b6943220b@xxxxxx-gateway.com...

    >
    > Hi Jon. Thanks for your reply. It works perfectly but i have to split a
    > string of thousands chars.
    > Now I try to explain exactly my problem.
    >
    > I have a file like this
    > string1of16chars
    > string2of16chars
    > string3of16chars
    > string4of16chars
    > string5of16chars
    > string6of16chars
    >
    > and so on.
    >
    > I have to join these lines grouping them three by three.
    > So the first line become
    >
    > string1of16charsstring2of16charsstring3of16chars
    >
    > the second
    > string4of16charsstring5of16charsstring6of16chars
    >
    > and so on
    >
    > I don't know how to solve this problem. Using add-content I've created
    > a kilometric one-line string and now I was looking for a way to split
    > every 48 chars but maybe with a loop it's possible to join the lines
    > three by three.
    >
    > Thanks again.
    >
    >
    > --
    > sardinian_guy

      My System SpecsSystem Spec

  6. #6


    Jon Guest

    Re: split string every tot chars

    Something like this might be a looping solution, though
    eg if such a large string proved too cumbersome etc....

    #-----------------------------------------
    $wholefile = Get-Content "C:\temp\RawData.txt"
    $Combinations = [int]($wholefile.length /3 )

    foreach ($Set in 1..$Combinations) {

    $Set1 = $wholefile[$(($Set-1)*3)].Trim()
    $Set2 = $wholefile[$((($Set-1)*3)+1)].Trim()
    $Set3 = $wholefile[$((($Set-1)*3)+2)].Trim()

    "$($Set1)$($Set2)$($Set3)"

    }

    #-----------------------------------------

    --
    Jon

      My System SpecsSystem Spec

  7. #7


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 03:25 AM
    italy

      Thread Starter

    Re: split string every tot chars

    Oh my god, this powershell rulez.
    Thanks Kiron. I didn't see your reply before. I spend a lot of time trying to write a comprehensible english. Maybe I was still writing when you posted.
    Your solution is perfect.

    Just another question if I can.
    Your second solution doesn't work on my powershell.
    I deduce I've version 1.
    I've found this link
    Download details: Windows PowerShell 2.0 CTP

    but I don't know what to do.
    Do I have to uninstall my powershell 1 or not?
    Thanks for your patience and thanks even to Jon.
    I love this forum.

      My System SpecsSystem Spec

  8. #8


    Jon Guest

    Re: split string every tot chars


    "sardinian_guy" <guest@xxxxxx-email.com> wrote in message
    news:c7b74b0980070d94e4ef2bfa7e41cc9e@xxxxxx-gateway.com...

    >

    > I've found this link
    > 'Download details: Windows PowerShell 2.0 CTP'
    > (http://www.microsoft.com/downloads/d...displaylang=en)
    >
    You can install that one, but this is the most recent version I think ...


    Windows PowerShell V2 Community Technology Preview 2 (CTP2)
    http://www.microsoft.com/downloadS/d...displaylang=en


    > but I don't know what to do.
    > Do I have to uninstall my powershell 1 or not?

    Yeah, you need to uninstall Powershell 1 first.


    --
    Jon




      My System SpecsSystem Spec

  9. #9


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 03:25 AM
    italy

      Thread Starter

    Re: split string every tot chars

    Quote Originally Posted by Jon View Post
    Something like this might be a looping solution, though
    eg if such a large string proved too cumbersome etc....

    #-----------------------------------------
    $wholefile = Get-Content "C:\temp\RawData.txt"
    $Combinations = [int]($wholefile.length /3 )

    foreach ($Set in 1..$Combinations) {

    $Set1 = $wholefile[$(($Set-1)*3)].Trim()
    $Set2 = $wholefile[$((($Set-1)*3)+1)].Trim()
    $Set3 = $wholefile[$((($Set-1)*3)+2)].Trim()

    "$($Set1)$($Set2)$($Set3)"

    }

    #-----------------------------------------

    --
    Jon
    Wow. Fantastic Jon. Thank you very much.
    In a single thread I've seen a lot of interesting thing.
    Compliments for your knowledge and your kindness.

      My System SpecsSystem Spec

  10. #10


    Jaykul Guest

    Re: split string every tot chars

    > I have to join these lines grouping them three by three.

    Use the -ReadCount parameter (and set $OFS to whatever text you want as the
    separator between lines). For your case, if an example file test.txt has
    these six lines:

    A1A2A3A4A5A6A7A8
    B1B2B3B4B5B6B7B8
    C1C2C3C4C5C6C7C8
    D1D2D3D4D5D6D7D8
    E1E2E3E4E5E6E7E8
    F1F2F3F4F5F6F7F8

    Then we run a command like this:

    > $ofs="";
    > get-content C:\Users\Joel\test.txt -readcount 3 | ForEach-Object { "
    > OUTPUT: '$_' " }
    OUTPUT: 'A1A2A3A4A5A6A7A8B1B2B3B4B5B6B7B8C1C2C3C4C5C6C7C8'
    OUTPUT: 'D1D2D3D4D5D6D7D8E1E2E3E4E5E6E7E8F1F2F3F4F5F6F7F8'

    That's what you want, right?

    --
    Joel "Jaykul" Bennett
    http://HuddledMasses.org/
    qotd: Economists can certainly disappoint you. One said that the economy
    would turn up by the last quarter. Well, I'm down to mine and it hasn't. --
    Robert Orben


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
split string every tot chars problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help to split a string into text and numeric values !! karsagarwal VB Script 10 04 Sep 2008
select-string and .split alicain PowerShell 3 12 Oct 2007
Using .split more than once on a string Marco Shaw PowerShell 6 09 Feb 2007
Format a string with non printable chars Romu PowerShell 2 26 Jan 2007
RE: Format a string with non printable chars Romu PowerShell 0 19 Jan 2007