提取包含完整字符串定界符的字符串
Extract string that contains delimiter of full string
我有一个小的批处理脚本,可以从 mysql.err
文件中获取临时密码。
但是当密码包含用于定界符匹配的 :
时我遇到了一个问题,结果密码将不会按原样获得..
还有其他方法可以正确获取密码吗?
@echo off
setlocal enableextensions enabledelayedexpansion
set mysql_dir=C:\database\mysql
echo.
echo.
echo Search For Current MySQL Password
FOR /F "tokens=4 delims=:" %%G in ('find /I "A temporary password is generated for root@localhost:" ^<%mysql_dir%\data\%ComputerName%.err') DO set temp_mysql_password="%%G"
echo.
echo.
echo Current MySQL password: %temp_mysql_password%
mysql.err
文件内容为:
2017-08-31T07:38:36.195916Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-31T07:38:36.205943Z 22 [Note] A temporary password is generated for root@localhost: v>dqu;;&)7:Y
2017-08-31T07:38:42.510215Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
问题在于密码
包含 :
的 v>dqu;;&)7:Y
** 将被提取为:
v>dqu;;&)7
.
这种没有分隔符的方法可以工作:
...
for /F "delims=" %%G in ('find /I "A temporary password is generated for" ^<%mysql_dir%\data\%ComputerName%.err') do (
set "temp_mysql_password=%%G"
set "temp_mysql_password=!temp_mysql_password:*A temporary password is generated for root@localhost: =!"
)
echo.
echo.
rem Note the ! around the variable
echo Current MySQL password: !temp_mysql_password!
它使用 here: Remove - Remove a substring using string substitution 解释的字符串操作。
请注意,它只适用于 root@localhost
,因为该字符串是搜索替换字符串的一部分。
要使其适用于任何 user@host
组合,您必须使用 3 个字符串替换而不是一个:
set "temp_mysql_password=!temp_mysql_password:*A temporary password is generated for =!"
set "temp_mysql_password=!temp_mysql_password:*@=!"
set "temp_mysql_password=!temp_mysql_password:*: =!"
输出:
Current MySQL password: v>dqu;;&)7:Y
无论如何要使用您的解决方案(使用 tokens
和 delims
),您可以连接所有分隔的部分,请参阅 。
如果您使用 "tokens=3* delims=:" 和下一个变量 %%H,您将获得未解析的密码。
星号代表输入的剩余部分,没有进一步拆分 delims。
@echo off & setlocal enableextensions enabledelayedexpansion
set mysql_dir=C:\database\mysql
echo.
echo.
echo Search For Current MySQL Password
FOR /F "tokens=3* delims=:" %%G in (
'find /I "A temporary password is generated for root@localhost:" ^<%mysql_dir%\data\%ComputerName%.err'
) DO set "temp_mysql_password=%%H"
set "temp_mysql_password=%temp_mysql_password:~1%"
echo.
echo.
echo Current MySQL password: %temp_mysql_password%
我有一个小的批处理脚本,可以从 mysql.err
文件中获取临时密码。
但是当密码包含用于定界符匹配的 :
时我遇到了一个问题,结果密码将不会按原样获得..
还有其他方法可以正确获取密码吗?
@echo off
setlocal enableextensions enabledelayedexpansion
set mysql_dir=C:\database\mysql
echo.
echo.
echo Search For Current MySQL Password
FOR /F "tokens=4 delims=:" %%G in ('find /I "A temporary password is generated for root@localhost:" ^<%mysql_dir%\data\%ComputerName%.err') DO set temp_mysql_password="%%G"
echo.
echo.
echo Current MySQL password: %temp_mysql_password%
mysql.err
文件内容为:
2017-08-31T07:38:36.195916Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-31T07:38:36.205943Z 22 [Note] A temporary password is generated for root@localhost: v>dqu;;&)7:Y
2017-08-31T07:38:42.510215Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
问题在于密码
包含 :
的 v>dqu;;&)7:Y
** 将被提取为:
v>dqu;;&)7
.
这种没有分隔符的方法可以工作:
...
for /F "delims=" %%G in ('find /I "A temporary password is generated for" ^<%mysql_dir%\data\%ComputerName%.err') do (
set "temp_mysql_password=%%G"
set "temp_mysql_password=!temp_mysql_password:*A temporary password is generated for root@localhost: =!"
)
echo.
echo.
rem Note the ! around the variable
echo Current MySQL password: !temp_mysql_password!
它使用 here: Remove - Remove a substring using string substitution 解释的字符串操作。
请注意,它只适用于 root@localhost
,因为该字符串是搜索替换字符串的一部分。
要使其适用于任何 user@host
组合,您必须使用 3 个字符串替换而不是一个:
set "temp_mysql_password=!temp_mysql_password:*A temporary password is generated for =!"
set "temp_mysql_password=!temp_mysql_password:*@=!"
set "temp_mysql_password=!temp_mysql_password:*: =!"
输出:
Current MySQL password: v>dqu;;&)7:Y
无论如何要使用您的解决方案(使用 tokens
和 delims
),您可以连接所有分隔的部分,请参阅
如果您使用 "tokens=3* delims=:" 和下一个变量 %%H,您将获得未解析的密码。
星号代表输入的剩余部分,没有进一步拆分 delims。
@echo off & setlocal enableextensions enabledelayedexpansion
set mysql_dir=C:\database\mysql
echo.
echo.
echo Search For Current MySQL Password
FOR /F "tokens=3* delims=:" %%G in (
'find /I "A temporary password is generated for root@localhost:" ^<%mysql_dir%\data\%ComputerName%.err'
) DO set "temp_mysql_password=%%H"
set "temp_mysql_password=%temp_mysql_password:~1%"
echo.
echo.
echo Current MySQL password: %temp_mysql_password%