0

I want to find words contain keyword from text files. Example text file(test.txt) contains below strings. Here is unmatched part. " Happynimall happy-monkey:2 happy-cat:5 " Here is unmatched part too. Here is unmatched part too.

So I tried powershell commands as follow.

$(gc test.txt).replace(' ',"`n")|select-string 'monkey','cat'

But the result failed to extract only matching word line like this. " Happynimall happy-monkey:2 happy-cat:5 "

By the way, cmd "findstr" works well in extracting matching word line generated in the pipe stream. powershell -command "$(gc test.txt).replace(' ',"""`n""")"|findstr "monkey cat" happy-monkey:2 happy-cat:5

Why cannot "select-string" extract matching word lines only? Why can "findstr" do that? How can I make select-string give same result with findstr? Is there anyone to help me understand about it and make select-string do that? Thanks in advance:-)

Thm Lee
  • 1,236
  • 1
  • 9
  • 12
  • ``replace(' ',"`n")`` -> `split(' ')` – user4003407 Jan 07 '18 at 09:39
  • @PetSerAl Thanks for your help. It works well, great. Well, would you plsease inform about the reason that replace() could not do that? – Thm Lee Jan 07 '18 at 09:56
  • If you replace space with new line in the string, then you will still have single string, which would be matched or not matched as whole. If you want to match them separately, then you should split, not replace. – user4003407 Jan 07 '18 at 10:27
  • @PetSerAl Well, thank you very much for detailed explanation. It is interesting what I've found is that command-sets with replace() are fairly quicker than those with split(). Thank you – Thm Lee Jan 07 '18 at 10:46

0 Answers0