View Single Post
Old 10-12-2008   #5 (permalink)
Kiron


 
 

Re: How do I filter rows in one csv file matching a value in another

Here are two ways, three including v2:

# v1 and v2: using the -Contains operator
$refVal = ipcsv RefVal.csv | % {$_.RefVal} | sort
ipcsv RefValContent.csv | ? {$refVal -contains $_.RefValC}

# v1: using the -Match operator and creating a regex pattern by joining
# the array through string expansion and the Output Field Separator '$OFS'
$refVal = ipcsv RefVal.csv | % {$_.RefVal} | sort
$pat = &{$ofs = '|'; "$refVal"}
ipcsv RefValContent.csv | ? {$_.RefValC -match $pat}

# v2: using the -Match operator and creating a regex pattern by joining
# the array through the -Join operator
$refVal = ipcsv RefVal.csv | % {$_.RefVal} | sort
ipcsv RefValContent.csv | ? {$_.RefValC -match ($refVal -join '|')}

--
Kiron
My System SpecsSystem Spec