Find 2 occurrences in powershell

Asked

Viewed 137 times

1

How can I find 2 occurrences together in a determining file using powershell?

$p = @("string1","string2")
get-content C:\log.txt | Select-String -Pattern $p 

Above, the expression returns me occurrences that have a string OR the other. It’s not what I want, I want to have a string And the other.

What are the ways to do this?

2 answers

4

You can do it like this

get-content C:\log.txt | Select-String -Pattern "string1" |  Select-String -Pattern "string2"

I don’t know if there’s a better way

1

  • For the purpose of exemplifying the answer, I replaced String1/String2 adding String3 :

String1 --> pizza

String2 --> chocolate

String3 --> soda pop


  • What suits me for this kind of task:

$i=@('.+(?=.*\bpizza\b)(?=.*\brefrigerante\b)(?=.*\bchocolate\b).*'); gc .\log.txt | sls $i  

  • Some examples diversified applied in the various searches in PowerShell

       # -------------------------------------------------------------------- #
       # listar o conteúdo do arquivo: .\log.txt                              #
         gc .\log.txt                                           # linha 0 - 8 #
       # -------------------------------------------------------------------- #
       # qualquer ocorrência de pizza, ou, refrigerante, ou chocolate         #
         gc .\log.txt | sls '(pizza|refrigerante|chocolate)'    # linha 1 - 8 #
         gc .\log.txt | sls 'pizza','refrigerante','chocolate'  # linha 1 - 8 #
         gc .\log.txt | sls 'chocolate','refrigerante','pizza'  # linha 1 - 8 #
         gc .\log.txt | sls 'refrigerante','chocolate','pizza'  # linha 1 - 8 #
         gc .\log.txt | sls '(?:pizza|refrigerante|chocolate)'  # linha 1 - 8 #
         gc .\log.txt | sls '(?:refrigerante|chocolate|pizza)'  # linha 1 - 8 #
         gc .\log.txt | sls '(?:chocolate|refrigerante|pizza)'  # linha 1 - 8 #
       # -------------------------------------------------------------------- #
       # chocolate | pizza | refrigerante numa ordem de ocorrências definida  #
       gc .\log.txt | sls 'chocolate.+pizza.+refrigerante'      # linha   - 7 #
       gc .\log.txt | sls 'refrigerante.+chocolate.+pizza'      # linha   - 8 #
       # -------------------------------------------------------------------- #
       # pizza + refrigerante + chocolate em qualquer ordem na mesma linha    #
         $i=@('.+(?=.*\bpizza\b)(?=.*\brefrigerante\b)(?=.*\bchocolate\b).*') #
         gc .\log.txt | sls $i                                 # linha 7 - 8  #
       # -------------------------------------------------------------------- #

inserir a descrição da imagem aqui


  • To fulfill any search among more than one string
  • To meet any search in a certain order of occurrence of strings
  • To meet any search in no certain order of occurrence of strings
  • Contents of the archive: .\log.txt (line 0 - 8)

0) frase sem torta de sanduíche com mandioquinha
1) frase com pizza de --------- com ------------
2) frase com ----- de chocolate com ------------
3) frase com ----- de --------- com refrigerante
4) frase com ----- de chocolate com refrigerante
5) frase com pizza de --------- com refrigerante
6) frase com pizza de chocolate com ------------
7) frase com chocolate de pizza com refrigerante
8) frase com refrigerante de chocolate com pizza

  • To any occurrence of pizza, or soda pop or chocolate on the line

  gc .\log.txt | sls '(pizza|refrigerante|chocolate)'    # linha 1 - 8 #
  gc .\log.txt | sls 'pizza','refrigerante','chocolate'  # linha 1 - 8 #
  gc .\log.txt | sls 'chocolate','refrigerante','pizza'  # linha 1 - 8 #
  gc .\log.txt | sls 'refrigerante','chocolate','pizza'  # linha 1 - 8 #
  gc .\log.txt | sls '(?:pizza|refrigerante|chocolate)'  # linha 1 - 8 #
  gc .\log.txt | sls '(?:refrigerante|chocolate|pizza)'  # linha 1 - 8 #
  gc .\log.txt | sls '(?:chocolate|refrigerante|pizza)'  # linha 1 - 8 #

  • Results:

1) frase com pizza de --------- com ------------
2) frase com ----- de chocolate com ------------
3) frase com ----- de --------- com refrigerante
4) frase com ----- de chocolate com refrigerante
5) frase com pizza de --------- com refrigerante
6) frase com pizza de chocolate com ------------
7) frase com chocolate de pizza com refrigerante
8) frase com refrigerante de chocolate com pizza

  • To the occurrences predefined of strings on the line:
    • Obeys the defined order of occurrences: chocolate + pizza + soda

gc .\log.txt | sls 'chocolate.+pizza.+refrigerante'

  • Results:

7) frase com chocolate de pizza com refrigerante

  • To the occurrences predefined of strings on the line:
    • Obeys the defined order of occurrences: soda + chocolate + pizza

gc .\log.txt | sls 'refrigerante.+chocolate.+pizza' 

  • Results:

8) frase com refrigerante de chocolate com pizza

  • To multiple strings in any order of occurrence on the line:

# pizza + refrigerante + chocolate em qualquer ordem na mesma linha    #
  $i=@('.+(?=.*\bpizza\b)(?=.*\brefrigerante\b)(?=.*\bchocolate\b).*') #
  gc .\log.txt | sls $i                                 # linha 7 - 8  #

  • Results:

7) frase com chocolate de pizza com refrigerante
8) frase com refrigerante de chocolate com pizza

Browser other questions tagged

You are not signed in. Login or sign up in order to post.