oriceon
oriceon

Reputation: 379

Extract string that contains delimiter of full string

I have a little batch script that gets the temporary password from mysql.err file.

But I have a problem when a password contains : that is used in delimiter match resulting password will not get as is..

Is there any other approach to get correctly the password?

@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%

The mysql.err file content is:

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).

And the problem is with that the password
v>dqu;;&)7:Y** that contains : will be extracted as:
v>dqu;;&)7.

Upvotes: 1

Views: 70

Answers (2)

user6811411
user6811411

Reputation:

If you'd used "tokens=3* delims=:" and the next for variable %%H you'd get the unparsed password.
The asterisk representing the remainder of the input without further splitting at 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%

Upvotes: 2

Andre Kampling
Andre Kampling

Reputation: 5631

This way without delimiters it will work:

...
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!

It uses string manipulation which is explained here: Remove - Remove a substring using string substitution.

Note that it will just work for root@localhost because that string is part of the string searched for substitution.

To make it work for any user@host combination you have to use 3 string substitutions instead of one:

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:*: =!"

Output:

Current MySQL password: v>dqu;;&)7:Y

To use your solution anyway (using tokens and delims) you could concate all the delimited parts, see here on SO how it works.

Upvotes: 1

Related Questions