Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - replace

Reply
 
Old 10-20-2007   #1 (permalink)
stratos


 
 

replace

Hej Powershell team.

In gui-help replace can be defined as - a specified number of times.
But how should i do, if i only will change the first space and not the other
space
in the following example ???

${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt

I need a real reference manual !!
--------------------------------------------------------------------

Replace

Definition: Returns a string in which a specified substring has been replaced
with another substring a specified number of times.

$a = "bxnxnx"
$a = $a -replace("x","a")

Regards
Torben Brønsholm
Denmark


My System SpecsSystem Spec
Old 10-20-2007   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: replace

stratos wrote:
Quote:

> Hej Powershell team.
>
> In gui-help replace can be defined as - a specified number of times.
> But how should i do, if i only will change the first space and not the other
> space
> in the following example ???
>
> ${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt
>
> I need a real reference manual !!
> --------------------------------------------------------------------
>
> Replace
>
> Definition: Returns a string in which a specified substring has been replaced
> with another substring a specified number of times.
>
> $a = "bxnxnx"
> $a = $a -replace("x","a")
>
> Regards
> Torben Brønsholm
> Denmark
>
See the same posting/subject from October 13th.

Sorry, it is going to be the same answer(s)... ;-)

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 10-20-2007   #3 (permalink)
Kiron


 
 

Re: replace

# to replace the first space in each line of the file
$pattern = '(.*?)( )(.*)'

(get-content C:\powerstart.txt) -replace $pattern,'$1;$3' > C:\powerstart1.txt

# to replace the first space in the file
# pipe its content to out-string and
# set the single line option in the regex

$pattern = '(?s)(.*?)( )(.*)'
(get-content C:\powerstart.txt | out-string) -replace $pattern,'$1;$3' > C:\powerstart1.txt

--
Kiron
My System SpecsSystem Spec
Old 10-21-2007   #4 (permalink)
stratos


 
 

RE: replace

Thanks for the help i got.
My final code was:

$pattern = '(?s)(.*?)( )(.*)'
${C:\powerstart.txt} -replace $pattern,'$1;$3' > C:\powerstart1.txt

And it works fine
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Replace PowerShell
Remove oem and replace with another oem Vista General
Replace IE7 with IE6? Vista General
Replace sed? PowerShell
Using .replace PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46