Thread: Find/Replace
View Single Post
Old 03-23-2007   #4 (permalink)
/\/\o\/\/ [MVP]


 
 

Re: Find/Replace

> I'm a little puzzled by where -match and -replace come from, as they
> are not native to powershell and do not appear to be members of the
> string class either. Do they belong to an object - I can't find the
> documentation in MSDN 2005?


they asre native powershell operators, for more information see :

Get-Help about_operator

Greetings /\/\o\/\/

"Duncan Smith" wrote:

> >
> > There isn't a native PowerShell cmdlet that replaces strings in files in place. However there is a -replace operator that provides the basic replace functionality. You will need to do this in several steps:
> >
> > $pattern = 'some search pattern - could be regex'
> > $replacement = 'some replacement potentially using capture groups like so $1 $2 or ${namedGroup}'
> > foreach ($file in (gci c:\temp\* -rec)) {
> > $text = get-content $file
> > if ($text -match $pattern) {
> > $text -replace $pattern, $replacement > $file
> > }
> >
> > }
> >

>
> That looks interesting, I was still thinking of a solution using the
> Win32 Unix tools find, egrep and sed, bur their sed implementation
> didn't update files in place so it all fell a bit flat..
>
> I'm a little puzzled by where -match and -replace come from, as they
> are not native to powershell and do not appear to be members of the
> string class either. Do they belong to an object - I can't find the
> documentation in MSDN 2005?
>
> Thanks,
>
> Duncan.
>
>

My System SpecsSystem Spec