tianyi
tianyi

Reputation: 377

Search for keywords from csv and display results

in the CSV Include the softname column

I need to search for a keyword containing 7-zip (or cmm) from it, if it is searched, display its version.

$csvtxt = @'
softname,version
7-Zip 18.05 (x64),18.05
Adobe Acrobat DC,18.011.20038
Adobe CMM,1.0
Adobe Flash Player 27 ActiveX,27.0.0.187
Adobe Flash Player 27 PPAPI,27.0.0.187
'@

$csv = ConvertFrom-Csv $csvtxt
$soft1 = "7-zip"
$soft2 = "cmm"

Upvotes: 0

Views: 42

Answers (2)

Lee_Dailey
Lee_Dailey

Reputation: 7479

you can use the way that -match works against a collection. that will give the objects that match the regex pattern.

what it does ...

  • create the data set to work with by faking reading in a CSV file
  • create the software list to check for
  • convert that list to an escaped regex OR pattern
  • run the -match operator against the collection using the pattern above
  • save the results to the $FoundSoftware collection
  • show it on screen

here's the code ...

# fake reading in a CSV file
#    in real life, use Import-CSV
$InStuff = @'
softname,version
7-Zip 18.05 (x64),18.05
Adobe Acrobat DC,18.011.20038
Adobe CMM,1.0
Adobe Flash Player 27 ActiveX,27.0.0.187
Adobe Flash Player 27 PPAPI,27.0.0.187
'@ | ConvertFrom-Csv

$SoftwareList = @(
    '7-zip'
    'cmm'
    )
$Regex_SL = $SoftwareList.ForEach({[regex]::Escape($_)}) -join '|'

$FoundSoftware = $InStuff -match $Regex_SL
# if you dislike the way that "-match" works directly on an array, use the next line 
#$FoundSoftware = $InStuff.Where({$_.SoftName -match $Regex_SL})

$FoundSoftware

output ...

softname          version
--------          -------
7-Zip 18.05 (x64) 18.05  
Adobe CMM         1.0  

if you want only the bare names, you can use Select-Object -ExpandProperty to get that. [grin]

Upvotes: 1

Prasoon Karunan V
Prasoon Karunan V

Reputation: 3043

You would use Where-Object cmdlet here.

ConvertFrom-Csv $CSVTxt | Where-Object -FilterScript { $_.SoftName -match "7-zip|cmm" } | Select-Object -Property Version

Read more about Where-Object here

Upvotes: 0

Related Questions