Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
GREP-ish
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.
SED-ish
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".
EXAMPLE:
==============DATA.TXT==============
Mary had a little lamb,
It's fleece was white as snow,
But the lamb made Mary mad,
So she ate it.
=================================
GREP-ish
cat DATA.TXT | where { $_ -match "Mary"}
returns the following:
Mary had a little lamb,
But the lamb made Mary mad,
SED-ish
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 :)
Technorati Tags: regular expressions, grep, sed, match, replace, text file, Mary, little lamb
Anonymous
July 08, 2007
So...I like GREP and SED from my old UNIX days and am VERY happy to see that PowerShell can do the sameAnonymous
October 18, 2008
Why didn't you just port the grep with all it's option, you just did invent the wheel, congrats.Anonymous
October 21, 2008
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 :)Anonymous
January 14, 2009
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.Anonymous
April 25, 2009
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.Anonymous
January 03, 2011
The comment has been removedAnonymous
January 03, 2011
Claus, 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: technet.microsoft.com/.../dd347736.aspx ZAnonymous
February 26, 2011
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?Anonymous
February 26, 2011
The comment has been removedAnonymous
February 28, 2011
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...Anonymous
March 03, 2015
The comment has been removedAnonymous
August 03, 2015
Won't work in Powershell 3.0 To fix it add parenthesis: (cat DATA.TXT) | % { $_ -replace "Mary","Susan" } > newfile.txtAnonymous
September 24, 2015
The comment has been removedAnonymous
November 24, 2015
@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.