Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Multiple -replaces

Closed Thread
 
Thread Tools Display Modes
Old 08-02-2007   #1 (permalink)
David
Guest


 

Multiple -replaces

Is there a way to pipe the output of a replace statement into another replace
statement? The following works, but I was hoping to eliminate the multiple
file read/writes by Get/Set-Content.

(Get-Content file.txt) | -replace "old string", "new string" | Set-Content
file.txt
(Get-Content file.txt) | -replace "old string1", "new string1" | Set-Content
file.txt
Old 08-02-2007   #2 (permalink)
dreeschkind
Guest


 

RE: Multiple -replaces

Do you mean something like this?

gc file.txt | foreach {
( $_ -replace "old string", "new string" ) -replace "old string1",
"new string1"
} | sc file.txt

--
greetings
dreeschkind

"David" wrote:

> Is there a way to pipe the output of a replace statement into another replace
> statement? The following works, but I was hoping to eliminate the multiple
> file read/writes by Get/Set-Content.
>
> (Get-Content file.txt) | -replace "old string", "new string" | Set-Content
> file.txt
> (Get-Content file.txt) | -replace "old string1", "new string1" | Set-Content
> file.txt

Old 08-02-2007   #3 (permalink)
Kiron
Guest


 

Re: Multiple -replaces

You can also use the switch statement, here are two approaches:

${c:file.txt} = $(switch -regex (${c:file.txt}) {
'old string' {$_ -replace $matches[0],'new string'}
'old string1' {$_ -replace $matches[0], 'new string1'}
default {$_}})

sc file.txt $(switch -regex -file ./file.txt {
'old string' {$_ -replace $matches[0],'new string'}
'old string1' {$_ -replace $matches[0], 'new string1'}
default {$_}})

--
Kiron
Old 08-02-2007   #4 (permalink)
David
Guest


 

RE: Multiple -replaces

Exactly!

Thank You

"dreeschkind" wrote:

> Do you mean something like this?

Old 08-03-2007   #5 (permalink)
Kiron
Guest


 

Re: Multiple -replaces

Either switch approach does not replace more than one pattern on each line.
Here's a way of that does, it uses corresponding pattern/substitution
arrays:

$pat = 'oldString0', 'oldString1'
$sub = 'newString0', 'newString1'

${c:file.txt} = ${c:file.txt} | % {
$i = 0; while ($i -lt $pat.length) {
$_ = $_ -replace $pat[$i], $sub[$i]; $i++} $_}

--
Kiron

Old 08-03-2007   #6 (permalink)
Hecks
Guest


 

Re: Multiple -replaces

"Kiron" wrote:

> Either switch approach does not replace more than one pattern on each line.
> Here's a way of that does, it uses corresponding pattern/substitution
> arrays:
>
> $pat = 'oldString0', 'oldString1'
> $sub = 'newString0', 'newString1'
>
> ${c:file.txt} = ${c:file.txt} | % {
> $i = 0; while ($i -lt $pat.length) {
> $_ = $_ -replace $pat[$i], $sub[$i]; $i++} $_}
>
> --
> Kiron
>


Thanks for the tip. That works nicely with hashtables, too:

$replace = @{"OLD1"="NEW1"; "OLD2"="NEW2"}

${c:file.txt} = ${c:file.txt} | % {
foreach ($key in $replace.keys) {$_ = $_ -replace $key, $replace[$key]}
$_
}

-Hecks
Old 08-03-2007   #7 (permalink)
David
Guest


 

Re: Multiple -replaces

Just a performace note:

I coded the four different methods suggested (by the way, Thank You) for
multiple replaces and used Measure-Command to obtain some exec times.

dreeschkind's foreach - 8.28 secs
Kiron's switch ${c:file.txt} - 0.61 secs
Kiron's switch sc file.txt - 0.65 secs
Kiron's pattern/substitution - 6.1 secs

"Kiron" wrote:

> Either switch approach does not replace more than one pattern on each line.
> Here's a way of that does, it uses corresponding pattern/substitution
> arrays:
>
> $pat = 'oldString0', 'oldString1'
> $sub = 'newString0', 'newString1'
>
> ${c:file.txt} = ${c:file.txt} | % {
> $i = 0; while ($i -lt $pat.length) {
> $_ = $_ -replace $pat[$i], $sub[$i]; $i++} $_}
>
> --
> Kiron
>

Old 08-04-2007   #8 (permalink)
Kiron
Guest


 

Re: Multiple -replaces

Thanks for the execution times. According to them the Switch with 'variable
namespace notation' ${c:file.txt} approach is the way to go for single
pattern per line and the pattern/substitution for multiple patterns per
line, but you did not measure Hecks' hash table which, according to some
test of my own, shaves some time off the pattern/substitution.

--
Kiron

Old 08-04-2007   #9 (permalink)
Kiron
Guest


 

Re: Multiple -replaces

Nicely? Yes. It seems to be faster than the pattern/substitution. Thanks.

--
Kiron
Old 08-05-2007   #10 (permalink)
Bruce Payette [MSFT]
Guest


 

Re: Multiple -replaces

This is even easier than you think :-) There are two features that help
here. First the replace operator works on collections. Second,
composing -replace operations just works. Here's an example. We'll start
with a file containing 3 lines:

PS (1) > gc text.txt
abc
abc
abc

Now we want to replace "a" with "A"

PS (2) > (gc text.txt) -replace "a","A"
Abc
Abc
Abc

Next, take the result of that operation, and replace "b" in the new
collection with "-b-".

PS (3) > (gc text.txt) -replace "a","A" -replace "b","-b-"
A-b-c
A-b-c
A-b-c

And finally, we'll replace "c" with "CCC"

PS (4) > (gc text.txt) -replace "a","A" -replace "b","-b-" -replace
"c","CCC"
A-b-CCC
A-b-CCC
A-b-CCC

Now let's save the result into a file using Set-Content:

PS (5) > (gc text.txt) -replace "a","A" -replace "b","-b-" -replace
"c","CCC" | sc text.txt

And verify that the file has been changed:

PS (6) > gc text.txt
A-b-CCC
A-b-CCC
A-b-CCC

and it has. So, as we've seen, composing replace operations is very simple.

-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book: http://manning.com/powershell

"David" <David@discussions.microsoft.com> wrote in message
news:BBFA3580-EC26-411A-809E-E4212D1B3DEF@microsoft.com...
> Is there a way to pipe the output of a replace statement into another
> replace
> statement? The following works, but I was hoping to eliminate the multiple
> file read/writes by Get/Set-Content.
>
> (Get-Content file.txt) | -replace "old string", "new string" | Set-Content
> file.txt
> (Get-Content file.txt) | -replace "old string1", "new string1" |
> Set-Content
> file.txt



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Round thing that replaces hourglass DeSade Vista General 1 2 Weeks Ago 06:06 AM
typing replaces text-fix Jim Macklin Live Mail 5 05-19-2008 02:58 PM
Microsoft replaces Vista kernel in SP1 thetruthhurts @homail.com Vista General 6 02-07-2008 08:16 PM
What replaces the Hibernate function James Matthews Vista General 7 07-24-2007 10:42 AM
É replaces ? sjr Vista mail 0 06-06-2007 09:17 PM








Vistax64.com 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 2005-2008

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 47 48 49 50