Help with Powershell regex
hi all. i'm trying break in regex following challenge:
i need parse , import-csv text file contains phone logs. here sample of 2 lines:
05/02/13 07:11pm 110 28 <i>new york 8457916382 0'00 00:02'25 tr
05/02/13 07:12pm 287 23 14124997643 00:01'15
i found need create custom headers , add delimiters. didn't want use commas because incoming (marked '<i>') caller id shows in last name,first name format, used | instead. replaced spaces preceed number |. problem don't want insert pipe between name of caller id , number following. suppose add criteria space peceeding number follows <i> , number of non numeric words should either ignored or replaced dash. alternately, search space preeceedin number has alphabet characters except or pm. don't know how formulate this.
here code far:
$log=get-childitem z:\|where-object name -like log*.txt|select-object -expandproperty fullname $workfile="$env:userprofile\desktop\workfile.txt" $workcsv="$env:userprofile\desktop\log.csv" $header="date","time","ext","co","dial number","ring","duration","acc code cd" $stuff=gc $log|?{-not($_ -notmatch '\a[0-9]')}|out-file $workfile $stuff=(gc $workfile) -replace '\s+(?=[0-9\<])',"|"|out-file $workfile $stuff=(gc $workfile) -replace '\s+(?=tr)',"|"|out-file $workfile $today=(get-date -format mm/dd/yy).tostring() import-csv -path $workfile -header $header -delimiter "|"|? date -eq $today i appreciate direction on either regex or more efficient way of tackling problem.
thanks much.
could give example of record caller id in last name,first name format looks like.
also example of expected output examples in .csv format helpful.
thks
this have far:
$output=@() $samples=@("05/02/13 07:11pm 110 28 <i>new york 8457916382 0'00 00:02'25 tr","05/02/13 07:12pm 287 23 14124997643 00:01'15") $samples | foreach{ $captures=[regex]::match($_,"(\d{2}/\d{2}/\d{2})\s(\d{2}:\d{2}(am|pm))\s+?(\d+)\s+?(\d+)\s+(\d*)\s*(\d{2,})\s+(\d+\'\d{2}|\s+)\s+(\d{2}:\d{2}\'\d{2})") $output+=new-object psobject -property @{ 'date'=$captures.groups[1] 'time'=$captures.groups[2] 'ext'=$captures.groups[4] 'co'=$captures.groups[5] 'dialnumber'=$captures.groups[6] 'number'=$captures.groups[7] 'ring'=$captures.groups[8] 'duration'=$captures.groups[9] } } $output | ft -autosize
dialnumber : <i>new york
time : 07:11pm
co : 28
ext : 110
ring : 0'00
duration : 00:02'25
number : 8457916382
date : 05/02/13dialnumber :
time : 07:12pm
co : 23
ext : 287
ring :
duration : 00:01'15
number : 14124997643
date : 05/02/13
inspired carlsberg.
Windows Server > Windows PowerShell
Comments
Post a Comment