2016-12-23 7 views
2

이 데이터의 변형 (텍스트 본문과 함께 'SVC'이후의 번호와 날짜)이 여러 번 나타나는 텍스트 파일이 있습니다. 데이터 문자열을 캡처 할 수는 있지만 일단 데이터가 있으면 이메일 주소를 찾아야합니다. 전자 메일은 4 ~ 9 행의 컨텍스트에 나타날 수 있습니다. 데이터를 분리하여 캡처 할 수 있도록 변수로 설정하는 방법을 알아낼 수 없습니다.Select-String - 데이터의 텍스트를 바꿔서 이메일 주소를 찾으십시오.

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._%+-][email protected][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 

내 예제 파일은 다음과 같이이다 : 나는에 대한 컨텍스트를 다시 호출하는 방법을 알아낼 수 없습니다 $Email = Select-String $_. -pattern '(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b)', $Email = Select-String $_.WLDir -pattern '(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b)'$Email = Select-String $_.Context -pattern '(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b)' 만에 상황에 이메일을 캡처 시도했습니다

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 protected] 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 

이메일 주소를 검색하십시오. 이 모든 일을 잘못했을 가능성도 있습니다. 아무도 내가 이것을 캡처하여 변수로 설정할 수있는 방법을 알고 있습니까?

+0

'$ _. Context.PostContext' –

+0

@ MathiasR.Jessen 오류가 발생하지 않지만 내 export.csv에 "전자 메일"의 빈 열이 표시됩니다. Context.PostContext -pattern '(\ b [A-Z0-9 ._ % + -] + @ [A-Z0-9 .-] + \. [AZ] { 2,4} \ b)''올바른 구문인가? 그 정규식에 이메일 주소를 찾는 데 문제가 있습니까? – Nate

+0

$ 전자 메일은 문자열이 아닌 matches 개체입니다. 그것이 무언가를 반환한다고 가정하면 먼저 값을 추출해야합니다. 첫 번째 끈이 그랬던 것처럼. 정규식의 대괄호는 여기에 필요하지 않은 그룹을 만듭니다. – Matt

답변

0

, 나는 상태로 포스트 맥락에서 모든 라인 0-9를 캡처하기로 결정했다. Excel 시트에서 this page의 계산을 =IF(O6="","",TRIM(RIGHT(SUBSTITUTE(LEFT(O6,FIND(" ",O6&" ",FIND("@",O6))-1)," ",REPT(" ",LEN(O6))),LEN(O6))))으로 사용하여 "O"열에서 "Q"열로 데이터를 가져옵니다. 모두의 도움에 감사드립니다.

0

이 시도 :이 정보를 캡처 할 수있는 정확한 방법을 못 찾았 때문에

$content = gc -path $path -Raw | Out-String 
$regex1 = [regex]"\[email protected]\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 
+0

이 모든 일은 이미'ForEach' 내에서 발생합니다. 돌아가서 선택한 문자열뿐만 아니라 전체 파일의 내용을 가져 오면 올바른 전자 메일 주소를 가져 오지 않을 것입니다. – Nate

+0

for-each는 발견 된 이메일의 인스턴스를 여러 개 얻는 것이 었습니다. 아이디어는 regex 객체를 사용하는 것이 었습니다. 그렇지 않으면 다음을 시도하십시오 : $ _ | Select-String $ regex -AllMatches | % {$ _. matches} | % {$ _. 값} – skrubber

+0

좋아요? $ regex = [정규식] \ w + [a-zA-Z0-9] @ \ w + [a-zA-Z0-9]. \ w + "''$ Email = $ _ | Select-String $ regex -AllMatches | % {$ _. matches} | % {$ _. 값}'? 저는 기본 변수'Select-String' ($ ElapsedTime 바로 아래)의 변수 목록에 넣을 것입니다. 이 경우 아무 것도 반환하지 않습니다. – Nate