i want to remove everything before url=https://
and after .ap.ngrok.io
mean I just want the code link d045-113-172-146-154
this is ngrok.log
t=2022-05-20T21:33:03 0700 lvl=info msg="no configuration paths supplied"
t=2022-05-20T21:33:03 0700 lvl=info msg="using configuration at default config path" path=C:\\Users\\Dell\\AppData\\Local/ngrok/ngrok.yml
t=2022-05-20T21:33:03 0700 lvl=info msg="open config file" path=C:\\Users\\Dell\\AppData\\Local\\ngrok\\ngrok.yml err=nil
t=2022-05-20T21:33:03 0700 lvl=info msg="starting web service" obj=web addr=199.0.0.1:4040
t=2022-05-20T21:33:03 0700 lvl=info msg="tunnel session started" obj=tunnels.session
t=2022-05-20T21:33:03 0700 lvl=info msg="client session established" obj=csess id=99dddre1f9ac
t=2022-05-20T21:33:03 0700 lvl=info msg="started tunnel" obj=tunnels name=command_line addr=http://localhost:80 url=https://d045-113-172-146-154.ap.ngrok.io
t=2022-05-20T21:33:18 0700 lvl=info msg="received stop request" obj=app stopReq="{err:<nil> restart:false}"
t=2022-05-20T21:33:18 0700 lvl=info msg="session closing" obj=tunnels.session err=nil
t=2022-05-20T21:33:18 0700 lvl=info msg="accept failed" obj=csess id=99dddre1f9ac err="reconnecting session closed"
I tried the code below but it print echo is off.
SetLocal EnableDelayedExpansion
set "input=ngrok.log"
set "substr=https://"
(
FOR /F "usebackq delims=" %%G IN ("%input%") DO (
set line=%%G
set id=!line:%substr%=!
)
)
echo %id% > yo.txt
set "substr2=.ap.ngrok.io"
(
FOR /F "usebackq delims=" %%G IN ("yo.txt") DO (
set line=%%G
set id2=!line:%substr2%=!
)
)
echo %id2%
CodePudding user response:
The task getting the hostname from the single line containing the full URL can be done with the following batch file:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "ngrok.log" exit /B 2
for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /R "url=https://..*\.ap\.ngrok\.io" "ngrok.log"') do (
set "Data=%%I"
setlocal EnableDelayedExpansion
set "Data=!Data:*https://=!"
set "Data=!Data:.ap.ngrok.io=|!"
for /F "delims=|" %%J in ("!Data!") do (
endlocal
echo The hostname is: %%J
set "HostName=%%J"
)
)
if defined HostName echo The last hostname "%HostName%" can be used here, too.
endlocal
The command line with FINDSTR is executed in background using one more cmd.exe
started with option /c
and the command line within '
appended as additional arguments. FINDSTR searches case-sensitive for lines containing a string matched by the regular expression url=https://..*\.ap\.ngrok\.io
in the file ngrok.log
in the current directory which of course can be any directory and must not be the directory containing the batch file.
The lines output by FINDSTR to handle STDOUT are captured by cmd.exe
processing the batch file and processed by FOR after findstr.exe
finished and the background command process closed itself.
FOR with /F
ignores empty lines and by default also all lines of which first space/tab separated substring starts with a semicolon and assigns otherwise just the first space/tab separated substring of the line to the specified loop variable I
for further processing it. That default line splitting behavior is not wanted here. Therefore an empty list of string delimiters and no end of line character is defined to get always each line with url=https://
and .ap.ngrok.io
assigned completely to the loop variable I
.
The line is assigned next to the environment variable Data
before enabling delayed expansion. Then delayed variable expansion is enabled as required for the next string operations.
There is first removed everything from the data line up to end of the first occurrence of https://
. It is not possible to search for a string containing an equal sign because of =
is interpreted as delimiter by command SET between search and replace string. The replace string is an empty string in this case.
There are next replaced all occurrences of case-insensitive interpreted search string .ap.ngrok.io
by a vertical bar which no hostname can contain to mark the end of the hostname in the remaining data line.
One more FOR /F command is used to get the string left to the (first) vertical bar. It is expected that the string does not start with ;
as that would be a very unusual hostname. The hostname is output and also assigned to the environment variable HostName
in the local environment defined with the second command line in the batch file.
If there is always just one hostname of interest in the log file, there could be used also the following code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "ngrok.log" exit /B 2
for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /R "url=https://..*\.ap\.ngrok\.io" "ngrok.log"') do (
set "Data=%%I"
setlocal EnableDelayedExpansion
set "Data=!Data:*https://=!"
set "Data=!Data:.ap.ngrok.io=|!"
for /F "delims=|" %%J in ("!Data!") do (
endlocal
set "HostName=%%J"
goto HaveName
)
)
for %%I in ("ngrok.log") do echo ERROR: There is no hostname found in the log file: "%%~fI"
exit /B 1
:HaveName
echo Found the hostname "%HostName%" in the log file "ngrok.log".
rem Insert here more command lines for processing.
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cmd /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
goto /?
if /?
set /?
setlocal /?