Find/Replace


  1.    22 Mar 2007 #1
    eric.eickhoff@sbcglobal.net Guest

    Find/Replace


    Hello,

    I am just learning PowerShell and need a little guidance.

    What I am trying to do is to search a directory recursively for files
    that contain a text string and modify the file(s) by replacing that
    string with other text.

    To do the search, I can use:

    dir -r c:\temp\* | Select-string "xyz"

    but from there, I am not sure how to go about doing the replace in
    each file found.

    Can anyone point me in the right direction to accomplish this?

    Thanks,

    -e

      My System SpecsSystem Spec

  2.    22 Mar 2007 #2
    Keith Hill Guest

    Re: Find/Replace


    <eric.eickhoff@sbcglobal.net> wrote in message news:1174589436.498853.183440@n76g2000hsh.googlegroups.com...
    > Hello,
    >
    > I am just learning PowerShell and need a little guidance.
    >
    > What I am trying to do is to search a directory recursively for files
    > that contain a text string and modify the file(s) by replacing that
    > string with other text.
    >
    > To do the search, I can use:
    >
    > dir -r c:\temp\* | Select-string "xyz"
    >
    > but from there, I am not sure how to go about doing the replace in
    > each file found.
    >
    > Can anyone point me in the right direction to accomplish this?


    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
    }
    }

    --
    Keith
      My System SpecsSystem Spec

  3.    23 Mar 2007 #3
    Duncan Smith Guest

    Re: Find/Replace


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

  4.    23 Mar 2007 #4
    /\/\o\/\/ [MVP] Guest

    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

  5.    23 Mar 2007 #5
    Duncan Smith Guest

    Re: Find/Replace


    On Mar 23, 11:57 am, /\/\o\/\/ [MVP] <o...@discussions.microsoft.com>
    wrote:
    > > 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.


    Thanks ;-)

      My System SpecsSystem Spec

  6.    28 Mar 2007 #6
    aaronlerch@gmail.com Guest

    Re: Find/Replace


    On Mar 22, 11:52 pm, "Keith Hill" <r_keith_h...@mailhot.nospamIdotcom>
    wrote:
    > <eric.eickh...@sbcglobal.net> wrote in messagenews:1174589436.498853.183440@n76g2000hsh.googlegroups.com...
    > > Hello,

    >
    > > I am just learning PowerShell and need a little guidance.

    >
    > > What I am trying to do is to search a directory recursively for files
    > > that contain a text string and modify the file(s) by replacing that
    > > string with other text.

    >
    > > To do the search, I can use:

    >
    > > dir -r c:\temp\* | Select-string "xyz"

    >
    > > but from there, I am not sure how to go about doing the replace in
    > > each file found.

    >
    > > Can anyone point me in the right direction to accomplish this?

    >
    > 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
    > }
    >
    > }
    >
    > --
    > Keith


    Thanks Keith!

    I think this function takes what you wrote and expresses it in an easy
    to reuse way (if not a little harder to read):

    function Replace-String($find, $replace, $includes)
    {
    get-childitem $includes | select-string $find -list |% { (get-
    content $_.Path) |% { $_ -replace $find, $replace } | set-content
    $_.Path }
    }

    http://www.aaronlerch.com/blog/2007/...-function.html

      My System SpecsSystem Spec

Find/Replace

Similar Threads
Thread Forum
find in files and replace
i am trying to replace values in files. I get this "in use" message. Is this because the gc command still has the file open? How do others do...
PowerShell
Reverse Find and Replace ?
Hi, I'm wanting to Reverse find and replace a particular character in a Powershell string. e.g. reverse find "b" in "abcabc" and replace with...
PowerShell
find/replace on file
Hello, I'm new to vbscript. I want to open a text file find 2 paragraph makrs and replace them with some text. When I run this code everything...
VB Script
find and replace a specific string in multiple files
basically, i have a vbscript which does a find and replace on single file, below i've shared the script code. ' replace.vbs Find Replacewith File...
VB Script
Advanced find and replace using VBScript
Howdy, I posted this in another section and was recommended to make this post in the VBScript section. Since I can't find any programs to do...
VB Script
Find & Replace in MSSQL Tables through PowerShell
Greetings, I am (very) new to PowerShell and I'm trying to absorb all it has to offer. :) Some of my colleagues have written a script to search...
PowerShell
Find and Replace Utility ?
Hello, I need a Find and Replace utility that works across multiple files. I found many such utilities, yet I need it to ask me, if I want to...
Vista General

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 18:11.
    .