![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 |
My System Specs![]() |
| | #2 (permalink) |
| | 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 |
My System Specs![]() |
| | #3 (permalink) |
| | 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 |
My System Specs![]() |
| | #4 (permalink) |
| | RE: Multiple -replaces Exactly! Thank You "dreeschkind" wrote: > Do you mean something like this? |
My System Specs![]() |
| | #5 (permalink) |
| | 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 |
My System Specs![]() |
| | #6 (permalink) |
| | 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 |
My System Specs![]() |
| | #7 (permalink) |
| | 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 > |
My System Specs![]() |
| | #8 (permalink) |
| | 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 |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Multiple -replaces Nicely? Yes. It seems to be faster than the pattern/substitution. Thanks. -- Kiron |
My System Specs![]() |
| | #10 (permalink) |
| | 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 |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Re: AMD Phenom X4 replaces X2 chip | Vista installation & setup | |||
| multiple search and replaces in a text file | VB Script | |||
| typing replaces text-fix | Live Mail | |||
| What replaces the Hibernate function | Vista General | |||
| É replaces ? | Vista mail | |||