![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Replace sed? I'd like to process text in a pipeline in a way similar to sed with multiple s commands. In particular, I'm trying to replace something like this: $P4CMD -p $P4PORT -P $P4PASSWD -u $USERNAME client -o $TEMPLATENAME \ | $SEDCMD -e "/^Client/s/$TEMPLATENAME/$CLIENTNAME/" \ -e "/^Host/s/:..*$/: $OWNERHOST/" \ -e "/^Owner/s/:..*$/: $USERNAME/" \ -e "/^Root/s/:..*$/: $CLIENTROOT/" \ -e "/^Options/s/ clobber/ noclobber/" \ -e "/^View/,$s/$TEMPLATENAME/$CLIENTNAME/" \ | $P4CMD -p $P4PORT -P $P4PASSWD -u $USERNAME client -i with a PowerShell idiom. That is, I want to pick out the lines that start with Host, Owner, etc. through Root, and replace the substring from the first ":" to the end of the line. For the Options I want to replace just clobber, if it's there, with noclobber. For each of the lines from the one starting with View until the end of the input, I want to replace the template name with the client name. I suspect that " $_ -replace" is my friend, but how do I restrict it to matching lines? Especially, is there a way to get the behavior of "/ ^View/,$" ? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Replace sed? "wscholine" <wscholine@gmail.com> wrote in message news:1174428928.198766.221680@y66g2000hsf.googlegroups.com... > I suspect that " $_ -replace" is my friend, but how do I restrict it > to matching lines? Split up the input by lines and then feed individual lines to $_. Get-Content by default spits out one line at a time down the pipeline so if you use it then you are set: gc foo.txt | % { $_ -replace <pattern>, <replacement> } If you are not reading a file (doesn't look like you are) then you can use the string split operator: "foo`nbar".split("`n") > Especially, is there a way to get the behavior of "/ > ^View/,$" ? I'm not familiar with this sed regex syntax. Can you explain? -- Keith |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Replace sed? On Mar 21, 2:07 am, "Keith Hill" <r_keith_h...@mailhot.nospamIdotcom> wrote: > "wscholine" <wschol...@gmail.com> wrote in message > > news:1174428928.198766.221680@y66g2000hsf.googlegroups.com... > > > I suspect that " $_ -replace" is my friend, but how do I restrict it > > to matching lines? > > Split up the input by lines and then feed individual lines to $_. > Get-Content by default spits out one line at a time down the pipeline so if > you use it then you are set: > > gc foo.txt | % { $_ -replace <pattern>, <replacement> } > > If you are not reading a file (doesn't look like you are) then you can use > the string split operator: "foo`nbar".split("`n") > > > Especially, is there a way to get the behavior of "/ > > ^View/,$" ? > > I'm not familiar with this sed regex syntax. Can you explain? > > -- > Keith It's an address range, it matches all of the lines beginning with one that starts with "View" and continuing to the end of the input. I am reading from a pipe (old style text pipe) in this case, but am interested in processing files also. > gc foo.txt | % { $_ -replace <pattern>, <replacement> } If I have a bunch of patterns to replace, do I do the " | % { ... } " multiple times? |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Replace sed? "wscholine" <wscholine@gmail.com> wrote in message news:1174485851.492476.306810@n76g2000hsh.googlegroups.com... >> gc foo.txt | % { $_ -replace <pattern>, <replacement> } > > If I have a bunch of patterns to replace, do I do the " | % { ... } " > multiple times? You could create an array of patterns and replacement strings and do something like this: gc foo.txt | % { $line = $_; for ($i=0; $i -lt $patterns.Length; i++) { $line = $line -replace $patterns[i], $replacement[i] }; $line} Note: I haven't tested this code. -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| replace | PowerShell | |||
| Replace | PowerShell | |||
| Remove oem and replace with another oem | Vista General | |||
| Replace IE7 with IE6? | Vista General | |||
| Using .replace | PowerShell | |||