how to get a value from a string line of a file
Question:
I have a log file which contains multiple lines and I want to get the value of the string WorkerThreads="20"
from specific line of the below log content
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="cvstatanalysis" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="logmanager" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="logmanager" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="remotefilecacheserver" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="remotefilecacheserver" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNCVINSTALLMGR" Type="SnapShot" BusyThreads="0" MaxThreads="200" ReadyTask="0" TaskList="0" WorkerThreads="1"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNCVINSTALLMGR" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="18" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="24"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="Delta" TaskAdded="308" TaskDone="308"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="scheduledreportingservice" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="scheduledreportingservice" Type="Delta" TaskAdded="0" TaskDone="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="NetworkServer" Instance="RecallService" Type="SnapShot" ConnectionCount="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="NetworkServer" Instance="RecallService" Type="Delta" ConnectionAdded="0" ConnectionRemoved="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="IOCPServerPool_RecallService" Type="SnapShot" BusyThreads="32" MaxThreads="32" ReadyTask="0" TaskList="0" WorkerThreads="32"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="IOCPServerPool_RecallService" Type="Delta" TaskAdded="0" TaskDone="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="RecallService" Type="SnapShot" BusyThreads="0" MaxThreads="100" ReadyTask="0" TaskList="0" WorkerThreads="1"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="RecallService" Type="Delta" TaskAdded="0" TaskDone="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="BrowseRouter" Type="SnapShot" ConnectionCount="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="BrowseRouter" Type="Delta" ConnectionAdded="0" ConnectionRemoved="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="MediaManager" Type="SnapShot" ConnectionCount="816"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="MediaManager" Type="Delta" ConnectionAdded="724" ConnectionRemoved="535"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="AppManager network access" Type="SnapShot" ConnectionCount="1269"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="IOCPServerPool_MediaManager" Type="SnapShot" BusyThreads="32" MaxThreads="32" ReadyTask="0" TaskList="0" WorkerThreads="32"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="AppManager network access" Type="Delta" ConnectionAdded="177" ConnectionRemoved="119"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="IOCPServerPool_MediaManager" Type="Delta" TaskAdded="0" TaskDone="0"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="BrowseRouter" Type="SnapShot" ConnectionCount="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMArMgrThPool" Type="SnapShot" BusyThreads="6" MaxThreads="20" ReadyTask="0" TaskList="0" WorkerThreads="20"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="BrowseRouter" Type="Delta" ConnectionAdded="0" ConnectionRemoved="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMArMgrThPool" Type="Delta" TaskAdded="1237" TaskDone="1237"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="ThreadPool" Instance="AppManager network access" Type="SnapShot" BusyThreads="0" MaxThreads="20" ReadyTask="0" TaskList="0" WorkerThreads="19"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMCloudVMManagementPool" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="ThreadPool" Instance="AppManager network access" Type="Delta" TaskAdded="8773" TaskDone="8773"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMCloudVMManagementPool" Type="Delta" TaskAdded="0" TaskDone="0"
In order to get to the specific lines I need to filter by searching the string Instance="sLNSQLServer61" Type="SnapShot"
which will give me the desired below output:
10000 2aa0 04/04 19:38:55 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:39:55 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:40:55 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:41:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:42:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:43:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:44:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:45:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="1" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="4"
10000 2aa0 04/04 19:46:57 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="13" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="15"
10000 2aa0 04/04 19:47:57 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="9" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="17"
10000 2aa0 04/04 19:48:57 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="17"
I can deal with coding till this point, what I need and still I don’t have any idea is after looking for the string Instance="sLNSQLServer61" Type="SnapShot"
and store the lines in a new file how can I get the number of the string WorkerThreads="17"
? in this case 17.
I have planned to store the numbers in a array and then obtain the highest to plot them by day
Answers:
To extract the value of WorkerThreads from the lines that contain the string Instance="sLNSQLServer61" Type="SnapShot", you can use regular expressions in Python. Here is an example code that should do what you need:
import re
filename = 'log.txt' # replace with your log file name
pattern = r'Instance="sLNSQLServer61" Type="SnapShot" .* WorkerThreads="(d+)"'
worker_threads = []
with open(filename) as f:
for line in f:
match = re.search(pattern, line)
if match:
worker_threads.append(int(match.group(1)))
print(worker_threads)
print(max(worker_threads))
This code uses a regular expression pattern that matches lines that contain the string Instance="sLNSQLServer61" Type="SnapShot" followed by any number of characters (.*) and the string WorkerThreads=", followed by one or more digits ((d+)) and a closing quote. The parentheses around d+ create a capturing group that captures the digits after WorkerThreads=".
For each line in the file, the code tries to match the pattern using re.search(). If a match is found, the code extracts the number of worker threads from the capturing group using match.group(1) and converts it to an integer using int(). The integer is then appended to a list worker_threads.
Finally, the code prints the list of worker thread counts and the maximum count using print().
Note that this code assumes that the log file is in the same format as the example lines you provided, with the WorkerThreads value appearing after the Instance and Type strings in the log line. If the log file has a different format, you may need to adjust the regular expression pattern to match the actual log lines.
I have a log file which contains multiple lines and I want to get the value of the string WorkerThreads="20"
from specific line of the below log content
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="cvstatanalysis" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="logmanager" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="logmanager" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="remotefilecacheserver" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="remotefilecacheserver" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNCVINSTALLMGR" Type="SnapShot" BusyThreads="0" MaxThreads="200" ReadyTask="0" TaskList="0" WorkerThreads="1"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNCVINSTALLMGR" Type="Delta" TaskAdded="0" TaskDone="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="18" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="24"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="Delta" TaskAdded="308" TaskDone="308"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="scheduledreportingservice" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 03/29 17:02:30 ### Context="cvd.exe" Module="ThreadPool" Instance="scheduledreportingservice" Type="Delta" TaskAdded="0" TaskDone="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="NetworkServer" Instance="RecallService" Type="SnapShot" ConnectionCount="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="NetworkServer" Instance="RecallService" Type="Delta" ConnectionAdded="0" ConnectionRemoved="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="IOCPServerPool_RecallService" Type="SnapShot" BusyThreads="32" MaxThreads="32" ReadyTask="0" TaskList="0" WorkerThreads="32"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="IOCPServerPool_RecallService" Type="Delta" TaskAdded="0" TaskDone="0"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="RecallService" Type="SnapShot" BusyThreads="0" MaxThreads="100" ReadyTask="0" TaskList="0" WorkerThreads="1"
15864 4ef4 03/29 17:02:41 ### Context="ClMgrS.exe" Module="ThreadPool" Instance="RecallService" Type="Delta" TaskAdded="0" TaskDone="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="BrowseRouter" Type="SnapShot" ConnectionCount="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="BrowseRouter" Type="Delta" ConnectionAdded="0" ConnectionRemoved="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="MediaManager" Type="SnapShot" ConnectionCount="816"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="NetworkServer" Instance="MediaManager" Type="Delta" ConnectionAdded="724" ConnectionRemoved="535"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="AppManager network access" Type="SnapShot" ConnectionCount="1269"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="IOCPServerPool_MediaManager" Type="SnapShot" BusyThreads="32" MaxThreads="32" ReadyTask="0" TaskList="0" WorkerThreads="32"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="AppManager network access" Type="Delta" ConnectionAdded="177" ConnectionRemoved="119"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="IOCPServerPool_MediaManager" Type="Delta" TaskAdded="0" TaskDone="0"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="BrowseRouter" Type="SnapShot" ConnectionCount="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMArMgrThPool" Type="SnapShot" BusyThreads="6" MaxThreads="20" ReadyTask="0" TaskList="0" WorkerThreads="20"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="NetworkServer" Instance="BrowseRouter" Type="Delta" ConnectionAdded="0" ConnectionRemoved="0"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMArMgrThPool" Type="Delta" TaskAdded="1237" TaskDone="1237"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="ThreadPool" Instance="AppManager network access" Type="SnapShot" BusyThreads="0" MaxThreads="20" ReadyTask="0" TaskList="0" WorkerThreads="19"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMCloudVMManagementPool" Type="SnapShot" BusyThreads="0" MaxThreads="10" ReadyTask="0" TaskList="0" WorkerThreads="0"
15848 4714 03/29 17:02:57 ### Context="AppMgrSvc.exe" Module="ThreadPool" Instance="AppManager network access" Type="Delta" TaskAdded="8773" TaskDone="8773"
15820 41dc 03/29 17:02:57 ### Context="MediaManager.exe" Module="ThreadPool" Instance="MMCloudVMManagementPool" Type="Delta" TaskAdded="0" TaskDone="0"
In order to get to the specific lines I need to filter by searching the string Instance="sLNSQLServer61" Type="SnapShot"
which will give me the desired below output:
10000 2aa0 04/04 19:38:55 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:39:55 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:40:55 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:41:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:42:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:43:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:44:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="0"
10000 2aa0 04/04 19:45:56 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="1" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="4"
10000 2aa0 04/04 19:46:57 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="13" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="15"
10000 2aa0 04/04 19:47:57 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="9" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="17"
10000 2aa0 04/04 19:48:57 ### Context="cvd.exe" Module="ThreadPool" Instance="sLNSQLServer61" Type="SnapShot" BusyThreads="0" MaxThreads="600" ReadyTask="0" TaskList="0" WorkerThreads="17"
I can deal with coding till this point, what I need and still I don’t have any idea is after looking for the string Instance="sLNSQLServer61" Type="SnapShot"
and store the lines in a new file how can I get the number of the string WorkerThreads="17"
? in this case 17.
I have planned to store the numbers in a array and then obtain the highest to plot them by day
To extract the value of WorkerThreads from the lines that contain the string Instance="sLNSQLServer61" Type="SnapShot", you can use regular expressions in Python. Here is an example code that should do what you need:
import re
filename = 'log.txt' # replace with your log file name
pattern = r'Instance="sLNSQLServer61" Type="SnapShot" .* WorkerThreads="(d+)"'
worker_threads = []
with open(filename) as f:
for line in f:
match = re.search(pattern, line)
if match:
worker_threads.append(int(match.group(1)))
print(worker_threads)
print(max(worker_threads))
This code uses a regular expression pattern that matches lines that contain the string Instance="sLNSQLServer61" Type="SnapShot" followed by any number of characters (.*) and the string WorkerThreads=", followed by one or more digits ((d+)) and a closing quote. The parentheses around d+ create a capturing group that captures the digits after WorkerThreads=".
For each line in the file, the code tries to match the pattern using re.search(). If a match is found, the code extracts the number of worker threads from the capturing group using match.group(1) and converts it to an integer using int(). The integer is then appended to a list worker_threads.
Finally, the code prints the list of worker thread counts and the maximum count using print().
Note that this code assumes that the log file is in the same format as the example lines you provided, with the WorkerThreads value appearing after the Instance and Type strings in the log line. If the log file has a different format, you may need to adjust the regular expression pattern to match the actual log lines.