powershell - Shell Programming Regex error -
i have text file , want display content file after searching word pattern.
my text file has following
hello 2013, a7 hello 2013, z8 errorprocessing adafda adasd adads adasdad adadsasd asdadsdasd adasdasdasda dadsadasda hello 2013, e1
my display should be
user: z8 error: errorprocessing adafda adasd adads adasdad adadsasd asdadsdasd adasdasdasda dadsadasda
i search errorproceesing pattern , display content unable display xyz008 dynamic word , may differ 1 text file another.
i need search
hello [0-9][0-9][0-9][0-9][a-z][0-9]
but not able desired output
i want implement in powershell have select-string -pattern [0-9][0-9][0-9][0-9][a-z][0-9] -allmatches -simplematch –
try following (substitute input filename file
):
do { # dummy loop can exit pipeline prematurely `break`. get-content file | % { if ($_ -match '^hello (\d{4}), ([a-z0-9]+)$') { # username found # save it. $username = $matches[2] } elseif ($_ -match '^errorprocessing ') { # error line found # output desired result. "user: $username`r`nerror: $_" # exit pipeline (via dummy loop). break } } } while ($false)
note: assumption want display one username, namely the recent 1 preceding error line.
- the loop necessary, because
select-string
not match across lines. - a username saved in same variable whenever encountered, storing most recent one.
- once error line found, recent user name , error line output, desired, , pipeline exited (in order exit pipeline
break
before processing lines, had wrapped in (dummy) loop).
Comments
Post a Comment