Select-String -上下文并在数据中查找电子邮件地址
Select-String -Context and find email address in data
我有一个文本文件,其中此数据的变体('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)'
的上下文中捕获电子邮件,但无法弄清楚如何回调上下文以在其中搜索电子邮件地址。我也很有可能做错了。有谁知道我如何捕获它并将其设置为变量?
试试这个:
$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
因为我从来没有找到一种准确的方法来捕获这些信息,所以我决定将 post 上下文中的所有 0-9 行都捕获到 Status 中。在 Excel sheet 上,我使用 =IF(O6="","",TRIM(RIGHT(SUBSTITUTE(LEFT(O6,FIND(" ",O6&" ",FIND("@",O6))-1)," ",REPT(" ",LEN(O6))),LEN(O6))))
的 this page 的计算将数据从 "O" 列提取到 "Q" 列,其中电子邮件属于。感谢大家的帮助。
我有一个文本文件,其中此数据的变体('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)'
的上下文中捕获电子邮件,但无法弄清楚如何回调上下文以在其中搜索电子邮件地址。我也很有可能做错了。有谁知道我如何捕获它并将其设置为变量?
试试这个:
$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
因为我从来没有找到一种准确的方法来捕获这些信息,所以我决定将 post 上下文中的所有 0-9 行都捕获到 Status 中。在 Excel sheet 上,我使用 =IF(O6="","",TRIM(RIGHT(SUBSTITUTE(LEFT(O6,FIND(" ",O6&" ",FIND("@",O6))-1)," ",REPT(" ",LEN(O6))),LEN(O6))))
的 this page 的计算将数据从 "O" 列提取到 "Q" 列,其中电子邮件属于。感谢大家的帮助。