Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Filter the contents of a file by an array of strings

Tags:

powershell

Riddle me this:

I have a text file of data. I want to read it in, and only output lines that contain any string that is found in an array of search terms.

If I were looking for just one string, I would do something like this:

get-content afile | where { $_.Contains("TextI'mLookingFor") } | out-file FilteredContent.txt

Now, I just need for "TextI'mLookingFor" to be an array of strings, where if $_ contains any string in the array, it is passed down the pipe to out-file.

How would I do that (and BTW, I'm a c# programmer hacking this powershell script, so if there is a better way to do my match above than using .Contains(), clue me in!)

like image 991
JMarsch Avatar asked Feb 11 '13 22:02

JMarsch


People also ask

How do I filter a list of objects in PowerShell?

There are many ways you can filter objects and their data in PowerShell. For example, you can use the Where-Object, Select-Object, Select-String, ForEach-Object, and Out-GridView cmdlets either separately or in conjunction with each other.

What does $_ mean in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

How do I list the contents of a file in PowerShell?

When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function. When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.


4 Answers

Try Select-String . It allows an array of patterns. Ex:

$p = @("this","is","a test")
Get-Content '.\New Text Document.txt' | Select-String -Pattern $p -SimpleMatch | Set-Content FilteredContent.txt

Notice that I use -SimpleMatch so that Select-String ignores special regex-characters. If you want regex in your patterns, just remove that.

For a single pattern I would probably use this, but you have to escape regex characters in the pattern:

Get-Content '.\New Text Document.txt' | ? { $_ -match "a test" }

Select-String is a great cmdlet for single patterns too, it's just a few characters longer to write ^^

like image 118
Frode F. Avatar answered Oct 08 '22 03:10

Frode F.


Any help?

$a_Search = @(
    "TextI'mLookingFor",
    "OtherTextI'mLookingFor",
    "MoreTextI'mLookingFor"
    )


[regex] $a_regex = ‘(‘ + (($a_Search |foreach {[regex]::escape($_)}) –join “|”) + ‘)’

(get-content afile) -match $a_regex 
like image 34
mjolinor Avatar answered Oct 08 '22 03:10

mjolinor


without regex and with spaces possible:

$array = @("foo", "bar", "hello world")
get-content afile | where { foreach($item in $array) { $_.contains($item) } } > FilteredContent.txt
like image 39
Nacht Avatar answered Oct 08 '22 01:10

Nacht


$a = @("foo","bar","baz")
findstr ($a -join " ") afile > FilteredContent.txt
like image 35
Ansgar Wiechers Avatar answered Oct 08 '22 03:10

Ansgar Wiechers