filter array

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of random commandline random commandline 4 months, 4 weeks ago.

  • Author
    Posts
  • #46667
    Profile photo of Nick Britton
    Nick Britton
    Participant

    I am looking for some assistance on how i can filter certian lines out of an array.

    Here is an example of what i am trying to do

    I have an exclude array

    $dbexclude = '.master.','.model.','.AdminDB.'

    I have an array filled by a command

    $dblist = & "C:\Program Files\Veritas\NetBackup\bin\bplist" -C $BackupHost -t 15 -s $BackupDate -R / | unique

    Here is some sample lines from that array:

    dbserver1.MSSQL7.dbserver1.db.old_MasterAuditDatabase.~.7.001of001.20160712114338..C:\
    dbserver1.MSSQL7.dbserver1.db.msdb.~.7.001of001.20160712114310..C:\
    dbserver1.MSSQL7.dbserver1.db.model.~.7.001of001.20160712114247..C:\
    dbserver1.MSSQL7.dbserver1.db.mhilltest.~.7.001of001.20160712114226..C:\
    dbserver1.MSSQL7.dbserver1.db.MASTER_AUDIT_DATABASE.~.7.001of001.20160712114149..C:\
    dbserver1.MSSQL7.dbserver1.db.master.~.7.001of001.20160712114126..C:\
    dbserver1.MSSQL7.dbserver1.db.Kronos_Punches.~.7.001of001.20160712114105..C:\

    What i would like to see :

    I would like to have a final array with values that would exclude matches form the exclude array so, from the example above the results should be:

    dbserver1.MSSQL7.dbserver1.db.old_MasterAuditDatabase.~.7.001of001.20160712114338..C:\
    dbserver1.MSSQL7.dbserver1.db.msdb.~.7.001of001.20160712114310..C:\
    dbserver1.MSSQL7.dbserver1.db.mhilltest.~.7.001of001.20160712114226..C:\
    dbserver1.MSSQL7.dbserver1.db.MASTER_AUDIT_DATABASE.~.7.001of001.20160712114149..C:\
    dbserver1.MSSQL7.dbserver1.db.Kronos_Punches.~.7.001of001.20160712114105..C:\

    Any thoughts on how i could accomplish this?

  • #46671
    Profile photo of Craig Duff
    Craig Duff
    Participant
    $dblist = #....# |
    Where-Object {
        $result = $true
        Foreach ($item in $dbexclude) {
            If($_ -like "*$item*") {
                $result = $false
                Break
            }
        }
        $result
    } 

    I'm on a tablet so there are probably typos. Where-object can have a script block that does whatever comparisons you want. Have the script block return true for objects that need to pass down the pipeline and false for the ones to filter out.

    • This reply was modified 4 months, 4 weeks ago by Profile photo of Craig Duff Craig Duff.
  • #46723
    Profile photo of random commandline
    random commandline
    Participant

    This will perform a case-sensitive comparison.

    $array -cnotmatch '(\.master|\.model|\.AdminDB)'

You must be logged in to reply to this topic.