Posted By

jarnaldich on 11/18/10


Tagged

regex search replace recursive regexp grep


Versions (?)

Search and Replace regexp in subdirectories


 / Published in: Windows PowerShell
 

replace foo -> bar in all .txt files down the tree

Dunno why, the syntax highlighter adds \3's to the code. Try plain text.

  1. # Select files
  2. $files = gci . -recurse | ? { ($_\3 -match "txt$") -and ((gc $_\3.VersionInfo.FileName) -match "the_sink") } | % { $_\3.VersionInfo.FileName }
  3.  
  4. # Back'em up, just in case:
  5. $files | % { cp $_\3 "$($_).bak" }
  6.  
  7. # Replace
  8. $files | %{ (gc $_\3) -replace "foo","bar" | sc -Path $_\3 }

Report this snippet  

You need to login to post a comment.