I have a need to extract a certain string from an output. What I do is spool all output from my command into a variable and then parse that variable. THe output varies in row count, it's never the same. But, the string in which I need to extract always has "logfilename" in the line.
Here is my code:
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET LF=^
::-- DO NOT REMOVE THE TWO EMPTY LINES ABOVE HERE --::
SET "CMD_OUTPUT="
SET "SEARCH=logFileName"
FOR /F "tokens=5 delims=/" %%A IN ('TYPE test.log ^| FINDSTR /RSIMC:"\%SEARCH%"') DO (
FOR /F Tokens^=1^ Delims^=^" %%a IN ("%%~A") DO (
IF DEFINED CMD_OUTPUT SET "CMD_OUTPUT=!CMD_OUTPUT!!LF!"
SET "CMD_OUTPUT=!CMD_OUTPUT!%%a"
)
)
echo %CMD_OUTPUT%
pause
Code: Select all
2021/01/07 06:13:12:103 EST [DEBUG] HttpMethodBase - Adding Host request header
2021/01/07 06:13:12:110 EST [DEBUG] header - >> "Authorization: Basic ********************************************[\r][\n]"
2021/01/07 06:13:12:110 EST [DEBUG] header - >> "Accept-Language: en-US[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "content-type: application/json[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "X-EPM_FUNCTION: EPM Automate[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "X-EPM_ACTION: Run Data Rule[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "X-EPM_OBJECT: jobName=GPM_Actual,startPeriod=Jan-20,importMode=REPLACE,fileName=inbox/batches/openbatchml/R_GPM_Actual_Jan-20_Dec-20_RR1.csv,exportMode=STORE_DATA,endPeriod=Dec-20,jobType=DATARULE[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "Host: domain.pbcs.us2.oraclecloud.com[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "Content-Length: 212[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] header - >> "[\r][\n]"
2021/01/07 06:13:12:111 EST [DEBUG] content - >> "{"jobName":"GPM_Actual","startPeriod":"Jan-20","importMode":"REPLACE","fileName":"inbox/batches/openbatchml/R_GPM_Actual_Jan-20_Dec-20_RR1.csv","exportMode":"STORE_DATA","endPeriod":"Dec-20","jobType":"DATARULE"}"
2021/01/07 06:13:12:465 EST [DEBUG] EntityEnclosingMethod - Request body sent
2021/01/07 06:13:15:237 EST [DEBUG] header - << "HTTP/1.1 200 OK[\r][\n]"
2021/01/07 06:13:15:237 EST [DEBUG] header - << "HTTP/1.1 200 OK[\r][\n]"
2021/01/07 06:13:15:240 EST [DEBUG] header - << "Date: Thu, 07 Jan 2021 11:13:12 GMT[\r][\n]"
2021/01/07 06:13:15:243 EST [DEBUG] header - << "Server: Oracle-Application-Server[\r][\n]"
2021/01/07 06:13:15:243 EST [DEBUG] header - << "P3P: X-CONTENT-TYPE-OPTIONS:nosniff[\r][\n]"
2021/01/07 06:13:15:243 EST [DEBUG] header - << "X-ORACLE-DMS-ECID: 005i4q6XYmf6yGFpR0T4id0002bV0000RI[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Vary: Accept-Encoding,User-Agent[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Set-Cookie: EPMJSESSIONID=11DcjO9GVJj_D4iSxc73CKwQ-wYlgtxpiISjBDhpF5CWrNB3i6BB!-380467034; path=/; HttpOnly;HttpOnly;Secure[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Set-Cookie: _WL_AUTHCOOKIE_EPMJSESSIONID=5zOdJI0osQPgG8eCfC.R; path=/; secure; HttpOnly;HttpOnly;Secure[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Transfer-Encoding: chunked[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Content-Type: application/json; charset=UTF-8[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Content-Language: en[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "Strict-Transport-Security: max-age=31536000 ; includeSubDomains[\r][\n]"
2021/01/07 06:13:15:246 EST [DEBUG] header - << "[\r][\n]"
2021/01/07 06:13:15:249 EST [DEBUG] CookieSpec - Unrecognized cookie attribute: name=HttpOnly, value=null
2021/01/07 06:13:15:249 EST [DEBUG] CookieSpec - Unrecognized cookie attribute: name=HttpOnly, value=null
2021/01/07 06:13:15:250 EST [DEBUG] HttpMethodBase - Cookie accepted: "EPMJSESSIONID=11DcjO9GVJj_D4iSxc73CKwQ-wYlgtxpiISjBDhpF5CWrNB3i6BB!-380467034"
2021/01/07 06:13:15:250 EST [DEBUG] CookieSpec - Unrecognized cookie attribute: name=HttpOnly, value=null
2021/01/07 06:13:15:250 EST [DEBUG] CookieSpec - Unrecognized cookie attribute: name=HttpOnly, value=null
2021/01/07 06:13:15:250 EST [DEBUG] HttpMethodBase - Cookie accepted: "_WL_AUTHCOOKIE_EPMJSESSIONID=5zOdJI0osQPgG8eCfC.R"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "1"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "4"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "a"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "[\r]"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "[\n]"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "{"executedBy":"name@client.com","jobStatus":"RUNNING","outputFileName":null,"processType":"COMM_LOAD_BALANCES","jobId":30585,"logFileName":"outbox/logs/GPM_30585.log","links":[{"rel":"self","href":"https://domain.pbcs.us2.oracle.com:443/aif/rest/V1/jobs/30585","action":"POST"}],"details":null,"status":-1}"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "[\r]"
2021/01/07 06:13:15:251 EST [DEBUG] content - << "[\n]"
2021/01/07 06:13:15:456 EST [DEBUG] content - << "0"
2021/01/07 06:13:15:456 EST [DEBUG] content - << "[\r]
I will always need to extract the string, the only different being outbox/logs/*.log. Is there a way better to extract that? Thank you!