Powershell find common strings varying number of arrays -
question follow powershell find common string in multiple files
following powershell code
goes through directory
for each file, extract ip addresses , store in multi-dimensional array
$match
- after iteration, go through each element in multi-dimensional array , split space, , store multi-dimensional array
$j
i able find intersection between $j[0]
, $j[1]
, i'm not sure how iteratively, on elements of $j
, array of ip address arrays.
see code
$i = $null $match = @() $j = @() $input_path = $null $output_file = "d:\script\common.txt" $directory = "d:\script\files" $regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’ get-childitem $directory | foreach-object{ $input_path = $directory + "\" + $_.name write-host $input_path $match += ,@(select-string -path $input_path -pattern $regex -allmatches | % { $_.matches } | % { $_.value }) } foreach ($i in $match){ $j += ,@($i.split(" ")) } $j[0] | sort | select -unique | {$j[1] -contains $_} | select -unique > $output_file
this easy. have two-dimensional array $j
, want find strings exist in elements of $j
. create temporary "total intersection" array out of $j[0]
run foreach on $j
, create intersection temporary. @ end it'll contain elements of columns contain.
# $j two-dimensional, , has unique elements $t=$j[0] $j | % { $i=$_ #rename avoid confusion if ($i -ne $j[0]) { $t = $t|where {$i -contains $_}} } # $t has intersection
Comments
Post a Comment