GREP and SED with PowerShell

Tips Search

GREP and SED with PowerShell

  • Comments 10

 

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

 

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

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

  • 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 :)

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

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

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

  • 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

    Z

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

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

    Z

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

Page 1 of 1 (10 items)
Leave a Comment
  • Please add 7 and 7 and type the answer here:
  • Post