GREP and SED with PowerShell


So…I like GREP and SED from my old UNIX days and am VERY happy to see that PowerShell can do the same type of functionality with a lot more power in most cases.  I though some of you might like some really simple examples of how to take a text file and do GREP-ish and SED-ish actions.



cat somefile.txt | where { $_ -match “expression”}


The command above will search each line of “somefile.txt” to see if it contains the regular expression “expression” and return the entire line if there is a match.






cat somefile.txt | %{$_ -replace “expression”,”replace”}


The command above will search each line of “somefile.txt” for the regular expression “expression” and replace it with the string “replace”.






Mary had a little lamb,

It’s fleece was white as snow,

But the lamb made Mary mad,

So she ate it.




cat DATA.TXT | where { $_ -match “Mary”}


returns the following:

Mary had a little lamb,

But the lamb made Mary mad,




cat DATA.TXT | % { $_ -replace “Mary”,”Susan” }


returns the following:

Susan had a little lamb,

It’s fleece was white as snow,

But the lamb made Susan mad,

So she ate it.


================ Edit 7/14 ==================== 

A question came up the the other day about putting these results into another text file.  Using redirection this is quite easy.  Here is how it is done:


cat DATA.TXT | % { $_ -replace “Mary”,”Susan” } > newfile.txt



enjoy 🙂


Comments (14)

  1. So…I like GREP and SED from my old UNIX days and am VERY happy to see that PowerShell can do the same

  2. Nicolae Namolovan says:

    Why didn’t you just port the grep with all it’s option, you just did invent the wheel, congrats.

  3. zainnab says:

    Ummm….yeah the point was to show how to do these kinds of activities with PowerShell not to reinvent GREP but thank you for playing 🙂

  4. Kelly says:

    you can also use select string

    cat data.txt | select-string -pattern "Mary"

    which also works on objects, like files so:

    dir -recurse | select-string -pattern "Mary"

    will do the grep, and tell you which file it found it in.

  5. Alf Nathan says:

    Thanks very much for this very useful info.

    It’s worth noting that you should use ‘ instead of " if you want to stop Powershell replacing things with variables in your regular expressions.

    e.g.  Get-Process | Select Name | sort -property Name | foreach { $_.Name -replace ‘^(.*)90$’, ‘$1ninety’ } will give you a list of sorted process names, changing any names that end with 90 to ninety, such as SQLAGENT90 to SQLAGENTninety.

  6. Claus Juul says:

    How do you pipe the output to a file name the same as the original – extension + .new + extension (data.txt become or data.txt becomes

  7. zainnab says:


    Check out the set-content command.  Although I didn't show it here get-content and set-content are very useful.  Here is a link to the command:…/dd347736.aspx


  8. Tym says:

    The use of ">" gave me quite a headache as it has weird consequences on the ouput file. I used your example on a xml config file for Tomcat, and I always ended up with a 500 Server error as soon as I used my modified config file.

    I finally tried something else which worked: cat DATA.TXT | % { $_ -replace "Mary","Susan" } | Set-Content newfile.txt . And just like that, my troubles were gone!

    I would be really interested in knowing the difference between ">" and "Set-Content", do you have any ideas?

  9. zainnab says:

    Hey Tym 🙂

    I haven't really dug into the differences between > and Set-Content.  Fundamentally they should be the same but > is much older so what you are experiencing is probably the result of the way it handles line termination or something like that.  Just a guess though.


  10. Tym says:

    I should have said that I tried copy/pasting the faulty file content into a working file (basically keeping just the file, but overwriting the content) and I had the same issue.

    To me it is closely related to the file in itself and not the content…

  11. The other thing I frequently use SED for is the selection of blocks of lines from a file. PowerShell doesn't do that out of the box but I wrote a script for that:…/sed-in-powershell.aspx

  12. Amir says:

    Won't work in Powershell 3.0

    To fix it add parenthesis:

    (cat DATA.TXT) | % { $_ -replace "Mary","Susan" } > newfile.txt

  13. sp says:

    So, I love the SED-ish (as well as Grep-ish) uses you've created here.  

    My question is regarding manipulation of an existing string within multiple files.  I do this in linux by moving to the directory containing the files to manipulate then typing a simple $> sed -i 's/old/new/g' * .

    When I execute PS>cat * | %{$_ -replace "expression","replace"} in a dir with multiple text files needing changed, this doesn't actually change the files, just what's been read in (then outputted to screen) by get-content.  How do I change the text files all in one fell swoop as I would with my trusted Sed?


  14. jimbobmcgee says:

    @sp – try piping the results of dir/gci to another foreach loop.  Something like…

       dir | %{ $f = $_; (cat $f) | %{ $_ -replace "Mary","Susan" } > "$f.tmp"; move "$f.tmp" $f -Force  }

    …might get you close.  It's not strictly in-place, but it should do the trick.  

    You can remove the `;move…` bit if you want to keep the original files.