Select-String -Context并在数据中查找电子邮件地址

时间:2016-12-23 14:57:04

标签: powershell variables select-string

我有一个文本文件,其中此数据的变体('SVC'之后的数字和之前的日期,以及文本正文)将多次出现。我可以捕获数据字符串,但是一旦我这样做,我需要在该数据中找到一个电子邮件地址。电子邮件可能出现在任何第4到9行的上下文中。我似乎无法弄清楚如何隔离数据并将其设置为变量以便可以捕获它。

Select-String $WLDir -pattern '(\d{2}:\d{2}) - (\d{2}:\d{2})(PMT[S|T]\d{8})' -Context 0,9 | ForEach-Object {
        $StartTime=[datetime]::ParseExact($_.Matches.Groups[1].Value,"HH:mm",$null)
        $EndTime=[datetime]::ParseExact($_.Matches.Groups[2].Value,"HH:mm",$null)
        $ElapsedTime = (NEW-TIMESPAN –Start $StartTime –End $EndTime).TotalHours
        $Email = Select-String $_. -pattern '(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)'
    [PSCustomObject]@{
        SO = $_.Matches.Groups[3].Value
        Topic = $_.Context.PostContext[0]
        Status = $_.Context.PostContext[1]
        ElapsedHrs = $ElapsedTime
        Email = $Email
    }
} | Export-Csv $ExportCsv -NoTypeInformation

我的示例文件是这样的:

  09:45 - 10:15SVC1234567 | Sev8 |437257 | COMPANY | Due: 12/28/2016
  WORK TITLE
  - - Preferred Customer (Y/N): Y Phone: 000-000-0000 ANY Hardware (Y/N): N 
  DATA on file (Y/N/NA): Y Contact: Person Name Full Address: 1234 PANTS 
  XING, RM/STE 100,NEWARK, NJ, 00000 - Hours: 8-5 Issue: Install admin 
  and others Fax Number: NA (required for all cases sent to LOCATION or 
  LOCATION_EXCPT Provider Groups) E-Mail address: email@location.com the 
  customer speak English? yes Escalation Approved By (Name/ID): Guy 
  aljdfs ITEM Product: PRODUCTNAME Group:THIS ONE Include 
  detailed notes below, including reason for severity: SCHEDULED WORK 
  ------------------------------ NOTES: -Cx requesting a tech on site -Cx 
  wants to install WS and wants to be assisted in other concerns

我尝试使用$Email = Select-String $_. -pattern '(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)'$Email = Select-String $_.WLDir -pattern '(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)'$Email = Select-String $_.Context -pattern '(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)'在上下文中捕获电子邮件,但无法弄清楚如何回调上下文以搜索其电子邮件地址。我也很有可能做错了。有谁知道如何捕获它并将其设置为变量?

2 个答案:

答案 0 :(得分:0)

试试这个:

$content = gc -path $path -Raw | Out-String
$regex1 = [regex]"\w+@\w+.\w+"
$regex2=[regex]"(?ms)(\d{2}:\d{2}) - (\d{2}:\d{2})(\D+)(\d+)(.*)"
$content | Select-String -pattern $regex2 | %{
$startTime = [datetime]::ParseExact(($regex2.Matches($content) | %{$_.Groups[1].Value}),"HH:mm",$null) 
$endTime = [datetime]::ParseExact(($regex2.Matches($content) | %{$_.Groups[2].Value}),"HH:mm",$null) 
$elapsedTime = (NEW-TIMESPAN –Start $startTime –End $endTime).TotalHours
$code = "PMT" + ($_.Matches.Groups[4].value) 
$remainingString = $_.Matches.Groups[5].Value
$topic = $remainingString.split("`n")[1] 
$status = $remainingString.split("`n")[2] 
$email = $regex1.Matches($remainingString).Value    

[PSCustomObject]@{
        SO = $code
        Topic = $topic
        Status = $status
        ElapsedHrs = $elapsedTime
        Email = $email
    }
} | Export-Csv "res.csv" -NoTypeInformation

答案 1 :(得分:0)

因为我从未找到捕获此信息的准确方法,所以我决定将后置上下文中的所有第0行捕获到状态。在Excel工作表上,我使用public class LoggerModel { // ... private string event; public string Event { get { return event; } set { event = FirstCharToUpper(value); } } // ... } this page的计算来从列" O"中提取数据。专栏#34; Q"电子邮件所属的地方。我感谢大家的帮助。