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 sed?

Reply
 
Old 03-20-2007   #1 (permalink)
wscholine


 
 

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 SpecsSystem Spec
Old 03-21-2007   #2 (permalink)
Keith Hill


 
 

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 SpecsSystem Spec
Old 03-21-2007   #3 (permalink)
wscholine


 
 

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 SpecsSystem Spec
Old 03-21-2007   #4 (permalink)
Keith Hill [MVP]


 
 

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 SpecsSystem Spec
Reply

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


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