PowerShell中找到共同的字符串数组不同数量 [英] Powershell find common strings varying number of arrays
问题描述
问题是一个跟进<一个href=\"http://stackoverflow.com/questions/30598988/powershell-find-common-string-in-multiple-files\">Powershell查找多个文件常见字符串
以下的PowerShell code
-
经过一个目录
-
为每个文件,提取IP地址,并存储在多维数组
$匹配
- 迭代之后,经过多维数组中的每个元素,并通过空间分割,并存储到另一个多维数组
$Ĵ
我能找到附加$ J [0]
和附加$ J [1]
,但之间的交集我不知道如何做到这一点反复,在附加$ J
,IP地址数组的数组。
请参阅code
$ I $ = NULL
$匹配= @()
$ J = @()
$ input_path = $空
$ OUTPUT_FILE =D:\\脚本\\ COMMON.TXT$目录=D:\\脚本\\文件
$正则表达式='\\ B \\Ð{1,3} \\。\\ D {1,3} \\。\\ D {1,3} \\。\\ D {1,3} \\ B'GET-ChildItem $目录|的foreach对象{ $ input_path = $目录+\\+ $ _。名称
写主机$ input_path
$匹配+ =,@(选择串-Path $ input_path -pattern $正则表达式-AllMatches |%{$ _。匹配} |%{$ _。值})
}的foreach($ i的$比赛){
$ J + =,@($ i.split())
}附加$ J [0] |排序| - 独特选择|其中{附加$ J [1] - 包含$ _} | - 独特选择GT&; $ OUTPUT_FILE
这是容易的。你说你有二维数组附加$ J
,并希望找到存在于附加$ J
中的所有元素的所有字符串。您创建一个临时的总交会数组了 $ j的[0]
然后运行附加$ J的foreach
并创建一个路口成暂时的。在最后它会仅包含那些所有的列包含的元素。
#$ j是二维的,并具有独特的元素
$ T =附加$ J [0]
附加$ J | %{
$ I = $ _ #rename以避免混淆
如果($ I $ -neĴ[0]){$ T = $ T |其中{$ I $ - 包含_}}
}
#$ T现在有你的交集
Question is a follow up to Powershell find common string in multiple files
Following PowerShell code
goes through a directory
for each file, extract IP addresses and store in multi-dimensional array
$match
- after iteration, go through each element in the multi-dimensional array and split by space, and store into another multi-dimensional array
$j
I am able to find the intersection between $j[0]
and $j[1]
, but I'm not sure how to do this iteratively, over all the elements of $j
, the 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 | where {$j[1] -contains $_} | select -Unique > $output_file
This is easy. You say you have two-dimensional array $j
and want to find all strings that exist in all elements of $j
. You create a temporary "total intersection" array out of $j[0]
then run foreach on $j
and create an intersection into that temporary. At the end it'll only contain those elements that all of the columns contain.
# $j is two-dimensional, and has unique elements
$t=$j[0]
$j | % {
$i=$_ #rename to avoid confusion
if ($i -ne $j[0]) { $t = $t|where {$i -contains $_}}
}
# $t now has your intersection
这篇关于PowerShell中找到共同的字符串数组不同数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!