Section 7.7. Match and Replace Content in a Text File


7.7. Match and Replace Content in a Text File

To change the content of a text file by replacing one string (e.g., "orig") with another (e.g., "update"):

     MSH> get-content file.txt | foreach-object { $_ -replace     "orig","update" } | set-content file.txt

Filters can be well-suited for repetitive changes to pipeline objects, such as a sequence of files:

     MSH D:\MshScripts> filter ReplaceInFile {     >>param($find,$replace)     >>get-content $_ | foreach-object { $_ -replace $find,$replace } | set-content $_     >>}     >>     MSH D:\MshScripts> get-childitem *.msh | ReplaceInFile "Author:" "Written by:"




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net