0

I'm trying to write a powershell cmdlet to find multiple words in lines in file. Example. I need to parse "word1", "word2", "word3" are in the same line of a file. I'm doing something wrong because I tried this with no success:

(gci -File -Filter FileName | Select-String -SimpleMatch word1, word2,word3) > outputFileName.txt

where FileName = name of file, outputFileName = generated file from my search of the three words. Thank you.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Ben Q
  • 187
  • 1
  • 1
  • 15
  • [Like this?](http://stackoverflow.com/questions/43450914/how-to-use-findstr-in-powershell-to-find-lines-where-all-words-in-the-search-str/43451196#43451196) – Mathias R. Jessen Apr 20 '17 at 16:14
  • Hi Mathais R Jessen. Can you please edit my code again. I overwrote your edit by accident. Thanks – Ben Q Apr 20 '17 at 16:16
  • Done. Please look at the link above, question is literally the same, except for the outputting to file part (which you've already figured out) – Mathias R. Jessen Apr 20 '17 at 16:18
  • Hi Mathais R. Jessen. I tried running the script again. It generates a the ouput file, but not the results I need. I'm looking for the output file to show only the lines containing "word1, word2,word3". Do I need to include logical "AND" to look for word1,word2, word3 from a line? Thank you. – Ben Q Apr 20 '17 at 16:34
  • Hey Ben, This should be flagged as a dupe of the question that Mathias linked. It is the exact same question that shows an elegant solution without the need to chaining `sls` statements. – Matt Apr 20 '17 at 18:43
  • Thank you, @Matt. If it is dupe, its ok. But I still don't see the link mentioned. – Ben Q Apr 20 '17 at 19:56
  • Do you not see the first comment on your question from Mathias? It also shows in the right hand pane under "linked" – Matt Apr 20 '17 at 19:59
  • Yeah, I see it now. I'm newbie navigating this page. Thanks . – Ben Q Apr 20 '17 at 20:02

2 Answers2

0

Select-String doesn't have any combination operator that I can think of. If your words were always in that order, then you could do -Pattern 'word1.*word2.*word3' as your match, but if they could be in any order that would get complex very quickly. Instead, I'd look at

.. | Select-String 'word1' | Select-String 'word2' | Select-String 'Word3'

so, all the lines which match word1. Out of those, the ones which match word2 somewhere. Out of that even smaller result, the ones which also match word3.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • @BenQ if this answer solved your problem, you should click the checkmark on the left, to mark it "accepted" – Mathias R. Jessen Apr 20 '17 at 18:00
  • one last question on this topic, can the multiple `Select-String` be refactored such as using `Select-AllStrings`. Tried it but Powershell keyword does not exist. – Ben Q Apr 20 '17 at 18:12
  • Isn't that exactly your original question, that my answer is answering? No it can't as far as I know, unless you can write your query it in regex, but if the word order can vary that's difficult, and if you want to use select string then you can chain them together ... etc. If you aren't wedded to select-string then you could potentially use `get-content` on each file, and then loop over the lines and test `($_ -match 'word1') -and ($_ -match 'word2') -and ($_ -match 'word3')` but that is longer, less tidy code. – TessellatingHeckler Apr 21 '17 at 17:09
0

try this:

$wordlist=@("word1", "word2", "word3")

Get-ChildItem "c:\temp\" -file |  %{$currentfile=$_.FullName; Get-Content $_.FullName | 
                    %{
                        $founded=$true
                        foreach ($item in $wordlist)
                        {
                            if (!$_.Contains($item))
                            {
                                $founded=$false
                                break
                            }
                        }

                        if ($founded)
                        {
                            $currentfile
                        }


                    }
}
Esperento57
  • 16,521
  • 3
  • 39
  • 45