Reputation: 13
I want to know the status of my running process from the detailed description using REGEX in Windows powershell scripting. I want to extract RUNNING from this string
Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00
Upvotes: 1
Views: 7351
Reputation: 54871
To extract RUNNING
or STOPPED
etc. you can try the following:
PS > $s = "Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00", "Name: Process2_name Started 2008-04-21 11:33 Status STOPPED Checkpoint Lag 00:00:00"
PS > $s | % { if ($_ -match "Status (.+) Checkpoint") {
#Return match from group 1
$Matches[1]
}
}
RUNNING
STOPPED
If you're reading a log-file, you can send the content of it directly to the test like this:
PS > Get-Content mylog.txt | % { if ($_ -match "Status (.+) Checkpoint") {
#Return match from group 1
$Matches[1]
}
}
This is extracting everything between "Status " and " Checkpoint", as long as it's at least 1 character (can be mulitple words also).
Upvotes: 1
Reputation: 1543
If you're looking for Windows processes, doesn't it make more sense to use Get-Process which supports properties and filtering, or the WMI equivalent, gwmi Win32_Process?
Might be way off the mark here so correct me if I'm mistaken!
Upvotes: 0
Reputation: 68243
Using -replace
$text = 'Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00'
$text -replace '.+\sStatus\s(\S+)\sCheckpoint.+','$1'
RUNNING
Upvotes: 3