Monday, 15 March 2010

parsing - Parse through text file and write out data -



parsing - Parse through text file and write out data -

i'm working on first steps towards creating powershell script read through printer logs (probably using get-wmi cmdlet), , parse through logs. afterwards, plan on having script output .txt file name of printer, counter of number of times printer used (if possible), , specific info found in logs.

in order this, i've decided seek working backwards. below little portion of logs like:

10 document 81, a361058/gpr0000151814_1: owned a361058 printed on r3556 via port ip_***.***.***.***. size in bytes: 53704; pages printed: 2 20130219123105.000000-300 10 document 80, a361058/gpr0000151802_1: owned a361058 printed on r3556 via port ip_***.***.***.***. size in bytes: 53700; pages printed: 2

working backwards , focusing on parsing first, i'd able "/grp", "r3446 (in general, r** printer name)", , counter shows how specific printer appeared in log files.

it has been while since lastly worked powershell, @ moment i've managed create in order seek accomplishing goal:

select-string -path "c:\documents , settings\a411882\my documents\scripts\print parse test.txt" -pattern "/gpr", " r****" -allmatches -simplematch

the code not produce errors, i'm unable output appear on screen see if i'm capturing /grp , printer name. @ moment i'm trying ensure i'm gathering right output before worrying counters. able assist me , tell me i'm doing wrong code?

thanks!

edit: fixed little error code causing no info appear on screen. @ moment code outputs entire 2 lines of test text instead of outputting /gpr , server name. new output following:

my documents\scripts\print parse test.txt:1:10 document 81, a361058/gpr0000151814_1: owned a361058 printed on r3556 via port ip_***.***.***.***. size in bytes: 53704; pages printed: 2 20130219123105.000000-300 documents\scripts\print parse test.txt:2:10 document 80, a361058/gpr0000151802_1: owned a361058 printed on r3556 via port ip_***.***.***.***. size in bytes: 53700; pages printed: 2

i'd seek having following:

/gpr, r****, count: ## (although i'm less concerned counter)

you can seek this. returns line when /gpr (and "on" "printed on") present.

get-content .\test.txt | % { if ($_ -match '(?:.*)(/gpr)(?:.*)(?<=on\s)(\w+)(?:.*)') { $_ -replace '(?:.*)(/gpr)(?:.*)(?<=on\s)(\w+)(?:.*)', '$1,$2' } }

output:

/gpr,r3556 /gpr,r3556

i'm sure there improve regex versions. i'm still learning :-)

edit easier read. regex still there extraction, filter out lines /gpr first using select-string instead:

get-content .\test.txt | select-string -simplematch -allmatches -pattern "/gpr" | % { $_.line -replace '(?:.*)(/gpr)(?:.*)(?<=on\s)(\w+)(?:.*)', '$1,$2' }

parsing powershell powershell-v2.0 printers

No comments:

Post a Comment