![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
| | #2 (permalink) |
| 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 |
| | #3 (permalink) |
| 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 |
| | #5 (permalink) |
| 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 |
| | #6 (permalink) |
| 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 |
| | #7 (permalink) |
| 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 > |
| | #8 (permalink) |
| 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 |
| | #10 (permalink) |
| 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 |
| |
| |
![]() |
| 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 |