需要提取8&使用PowerShell从40,000封电子邮件中获得9位数的文件编号

时间:2018-02-14 22:44:20

标签: powershell vbscript extraction

我试图提取8&来自40,000封已保存为.txt文件的电子邮件的9位数文件编号。文件编号可以出现在电子邮件中的任何位置......(它不是标准格式),但长度应始终为8或9位。文件编号也可以通过几种不同的方式格式化如:xxx xx xxxx,xxx-xx-xxxx,xxxxxxxxx,8位#s:YY YYY YYY,YY-YYY-YYY,YYYYYYYY。我创建了一个PowerShell脚本,它读取文本文件,提取与所述模式匹配的文件编号,并创建&将它们保存到.csv文件中。

问题:如果文件#上有任何文本正在进行,则脚本无法获取文件#。它还会抓取其他文本(在File#之后的同一行)。我只需要与设定模式完全匹配。

解决方案不需要在PowerShell中,如果在vbscript中有更好的解决方案,我也会对此持开放态度。

当前脚本如下:

$Num = @()

$Num += Select-String -Path "$PSSCRIPTROOT\text.txt" -Pattern '\d{8}$|^\d{2}\s\d{3}\s\d{3}$|^\d{2}-\d{3}-\d{3}$'


$Num += Select-String -Path "$PSSCRIPTROOT\text.txt" -Pattern '\d{9}$|^\d{3}\s\d{2}\s\d{4}$|^\d{3}-\d{2}-\d{4}$'


ForEach ($Matches in $Num){


$Found = $Matches.ToString().Split(":")
$o = new-object PSObject
$o | add-member NoteProperty "FoundOnLine" $Found[2]
$o | add-member NoteProperty "Number" $Found[3]


$o | export-csv "$PSscriptroot\FoundNumbers.csv" -notypeinformation -Append 
Write-Output $o

请帮助!

1 个答案:

答案 0 :(得分:1)

这应该可以做到这一点......

$File = "$PSSCRIPTROOT\text.txt"
$Pattern = '\d\d(\s|-)*\d(\s|-)*\d(\s|-)*\d{4,5}'

Select-String -Path $File -Pattern $Pattern -AllMatches | 
    Select-Object -ExpandProperty Matches |
        Select-Object -ExpandProperty Value