none
Find multiple strings in text files powershell RRS feed

  • Question

  • I'm trying to create a script that searches through a folders and subfolders searching all the text files for 2 strings. If a file is found then it copys this to another folder. I'm able to do it for 1 string but i can't seem to figure out a way to do an AND in it. (Using -and in the select-string doesnt seem to work)

    This is what I have so far(Quite sure the -or doesn't work)

    $user = "domain\username"
    Invoke-Command -ComputerName Server -ScriptBlock { 
    Get-ChildItem -recurse -Include *.txt  -path "File Location" | 
    Select-String -pattern ("TAX=1+005+0950" -or "TAX=1+5+0950+0") -SimpleMatch  |  
    Copy-Item -Destination "Destination Location"
    } -credential $user

    I'm needing to search for either of the ones in the code above AND either of these 2 "TAX=1+5+0600" "TAX=1+005+0600"

    Any idea how I can do this?

    So basically I want to be able to do

    Select-String -pattern (TAX=1+005+0950 OR TAX=1+5+0950+0) AND (TAX=1+005+0600 OR TAX=1+5+0600)
    Wednesday, June 1, 2016 8:46 AM

Answers

  • Kelix Aleir, have you tried the one I suggested? 

    It does exactly what you say - finds files where one of the first pair of strings is present and one of the second pair. 

    Ups! Just noticed one of the filters was misspelled. Try these:

    Get-ChildItem -recurse -Include *.txt |
    where { $_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch} | 
    where { $_ | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch} 

    Get-ChildItem -recurse -Include *.txt | 
    where { $_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch} | 
    Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch

    Regards

    Wednesday, June 1, 2016 11:22 AM

All replies

  • Use the Regex

    'TAX=\d\+\d+\+\d+(\+\d)?'

    It returns True for all 4 cases. "TAX=1+005+0950","TAX=1+5+0950+0","TAX=1+005+0600","TAX=1+5+0600"

    Wednesday, June 1, 2016 8:56 AM
  • Use the Regex

    'TAX=\d\+\d+\+\d+(\+\d)?'

    It returns True for all 4 cases. "TAX=1+005+0950","TAX=1+5+0950+0","TAX=1+005+0600","TAX=1+5+0600"

    If you want to know more about regular expression (the best way to do what you want), take a look :

    http://ss64.com/ps/syntax-regex.html

    Wednesday, June 1, 2016 9:31 AM
  • Hi,

    I believe the topic starter has stricter conditions than ones checked by the RegExp above. For example the 'TAX=1+004+0950' passes that RegExp, but was not mentioned by Kelix Aleir.

    You can try to use the following search line instead of the one you have:

    Get-ChildItem -recurse -Include *.txt |
    where { $_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch} | 
    where { $_ | Select-String -pattern ('TAX=TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch} 

    Or, if you prefer the output format that you previously had:

    Get-ChildItem -recurse -Include *.txt | 
    where { $_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch} | 
    Select-String -pattern ('TAX=TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch
    Regards


    • Edited by Avendil Wednesday, June 1, 2016 9:56 AM
    Wednesday, June 1, 2016 9:38 AM
  • Get-ChildItem -recurse -Path C:\Test -Include *.txt |
        where { ($_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch) -or 
                 ($_ | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch ) } | 
    Copy-Item -Destination C:\Test\

    If any of the above doesn't work try this

    Added -or to Avendil Script


    Wednesday, June 1, 2016 10:08 AM
  • This will give you all the required  files.

    (Get-ChildItem -path "filelocation" -File -include *.txt -Recurse).Name -match 'TAX=\d\+\d+\+\d+(\+\d)?'

    Wednesday, June 1, 2016 10:13 AM
  • Get-ChildItem -recurse -Path C:\Test -Include *.txt |
        where { ($_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch) -or 
                 ($_ | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch ) } | 
    Copy-Item -Destination C:\Test\

    If any of the above doesn't work try this

    Added -or to Avendil Script


    I believe, we need to match the file names.This will work

    Get-ChildItem -recurse -Path C:\Test -Include *.txt |
        where { ($_.Name | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch) -or 
                 ($_.Name | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch ) } | 
    Copy-Item -Destination C:\Test\


    Wednesday, June 1, 2016 10:16 AM
  • Did anyone actually read the question. The search is for two strings in a file.  If both strings are in the file then copy the file.


    \_(ツ)_/

    Wednesday, June 1, 2016 10:19 AM
    Moderator
  • Did anyone actually read the question. The search is for two strings in a file.  If both strings are in the file then copy the file.


    \_(ツ)_/

    That is the reason i gave regex.
    Wednesday, June 1, 2016 10:23 AM
  • Hi Mukesh
    He only wants to search for this in a text file
    'TAX=1+005+0950 OR TAX=1+5+0950+0' AND 'TAX=1+005+0600 OR TAX=1+5+0600'
    If u run ur command
    If any text file conatins 'TAX=1+010+0900' or 'TAX=1+010+5000'
    it would be a match
    Wednesday, June 1, 2016 10:25 AM
  • Thanks for help guys, I tried a few of these but none work the way I needed.

    I probably didn't explain it very well.

    Usually the text file contains either Tax=1+005_0950 or Tax=1+005+0600, however a few files have messed up and they contain both.

    The idea of the script is to find the text files that only contain both strings (Not one of the other)

    The reason I have it like:

    (TAX=1+005+0600 OR TAX=1+5+0600)

    is because this is the 2 different formats it could be, the simplest I can explain it is

    (TAX=1+005+0950 OR TAX=1+5+0950+0) AND (TAX=1+005+0600 OR TAX=1+5+0600)
    Wednesday, June 1, 2016 10:37 AM
  • Did anyone actually read the question. The search is for two strings in a file.  If both strings are in the file then copy the file.


    \_(ツ)_/

    That is the reason i gave regex.

    This of your searches a name not a file:

        where { ($_.Name | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch) -or
                
    ($_.Name | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch ) } |

    To search the file you need to read the contents of the file:

        where { ($_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch) -or
                
    ($_ | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch ) } |

    The issue is not how to"or" but how to "and" to search for the existence of BOTH strings.

    The pattern array is an implied "or".




    \_(ツ)_/

    Wednesday, June 1, 2016 10:43 AM
    Moderator
  • Get-ChildItem -recurse -Path C:\Test -Include *.txt |
        where {( ($_ | Select-String -pattern ('TAX=1+005+0950') -SimpleMatch) -or
                 ($_ | Select-String -pattern ('TAX=1+5+0950+0' ) -SimpleMatch) )-and
                 ( ($_ | Select-String -pattern ('TAX=1+005+0600' )  -SimpleMatch ) -or
                   ($_ | Select-String -pattern ('TAX=1+5+0600') -SimpleMatch)  ) }
    Edit - Try now

    Wednesday, June 1, 2016 10:50 AM
  • Here is a hint as to how to do this.  You need to work with the complete file and not just match by line:

    dir  |?{(cat $_ -raw) -match '(?=.*txt)(?=.*test)'}

    This gives a list of files that contain both "txt" and "test"


    \_(ツ)_/

    Wednesday, June 1, 2016 10:53 AM
    Moderator
  • Kelix Aleir, have you tried the one I suggested? 

    It does exactly what you say - finds files where one of the first pair of strings is present and one of the second pair. 

    Ups! Just noticed one of the filters was misspelled. Try these:

    Get-ChildItem -recurse -Include *.txt |
    where { $_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch} | 
    where { $_ | Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch} 

    Get-ChildItem -recurse -Include *.txt | 
    where { $_ | Select-String -pattern ('TAX=1+005+0950','TAX=1+5+0950+0') -SimpleMatch} | 
    Select-String -pattern ('TAX=1+5+0600','TAX=1+005+0600') -SimpleMatch

    Regards

    Wednesday, June 1, 2016 11:22 AM
  • Thank you! Much appreciated, this worked. 
    Wednesday, June 1, 2016 3:00 PM