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.
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" <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
Also:
$str = 'aabbcc'
# v1
[regex]::split($str, '(.{2})') | ? {$_}
# v2
$str -split '(.{2})' | ? {$_}
--
Kiron
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.![]()
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
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
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.
"sardinian_guy" <guest@xxxxxx-email.com> wrote in message
news:c7b74b0980070d94e4ef2bfa7e41cc9e@xxxxxx-gateway.com...
>You can install that one, but this is the most recent version I think ...
> I've found this link
> 'Download details: Windows PowerShell 2.0 CTP'
> (http://www.microsoft.com/downloads/d...displaylang=en)
>
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
> 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:
OUTPUT: 'A1A2A3A4A5A6A7A8B1B2B3B4B5B6B7B8C1C2C3C4C5C6C7C8'
> $ofs="";
> get-content C:\Users\Joel\test.txt -readcount 3 | ForEach-Object { "
> OUTPUT: '$_' " }
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
| 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 |