我想洗掉之前url=https://和之后.ap.ngrok.io的所有內容意味著我只想要代碼鏈接d045-113-172-146-154
這是 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"
我嘗試了下面的代碼,但它列印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%
uj5u.com熱心網友回復:
從包含完整 URL 的單行獲取主機名的任務可以使用以下批處理檔案完成:
@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
帶有FINDSTR的命令列是在后臺執行的,它使用了另外一個cmd.exe以選項開頭的選項/c,并將其中的命令列'作為附加引數附加。FINDSTRurl=https://..*\.ap\.ngrok\.io區分大小寫搜索包含與當前目錄中的檔案中的正則運算式匹配的字串的行,該檔案ngrok.log當然可以是任何目錄,但不能是包含批處理檔案的目錄。
FINDSTR輸出的用于處理STDOUTcmd.exe的行通過處理批處理檔案被捕獲,并在完成后由FORfindstr.exe處理,后臺命令列程自行關閉。
FOR with/F忽略空行,默認情況下也忽略第一個空格/制表符分隔的子字串以分號開頭的所有行,否則僅將該行的第一個空格/制表符分隔的子字串分配給指定的回圈變數I以進行進一步處理。這里不需要默認的行拆分行為。因此,定義了一個空的字串定界符串列并且沒有行尾字符,以始終獲取每一行url=https://并.ap.ngrok.io完全分配給回圈變數I。
Data在啟用延遲擴展之前,該行被分配到環境變數旁邊。然后根據需要為下一個字串操作啟用延遲變數擴展。
首先洗掉從資料行到第一次出現的結尾的所有內容https://。無法搜索包含等號的字串,因為在搜索和替換字串之間=被命令SET解釋為分隔符。在這種情況下,替換字串是一個空字串。
接下來將所有出現的不區分大小寫的解釋搜索字串替換為.ap.ngrok.io主機名不能包含的豎線,以標記剩余資料行中主機名的結尾。
另一個FOR /F命令用于將字串留在(第一個)豎線。預計該字串不會以它開頭,;因為這將是一個非常不尋常的主機名。主機名被輸出并分配給HostName本地環境中的環境變數,該環境變數由批處理檔案中的第二個命令列定義。
如果日志檔案中始終只有一個感興趣的主機名,則還可以使用以下代碼:
@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
要了解所使用的命令及其作業原理,請打開命令提示符視窗,在其中執行以下命令,并仔細閱讀每個命令顯示的所有幫助頁面。
cmd /?echo /?endlocal /?exit /?findstr /?for /?goto /?if /?set /?setlocal /?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/480832.html
標籤:批处理文件
