- Works only in Windows!
Create batch-file with name «findrow.bat» and save the following code:
@echo off rem (c) 18.09.2015 rem infile - file in which you want to search a string set infile=%1 set searchstring=%2 rem offset - include additional lines to (example, -3) or after (example, 3) search string set offset=%3 rem get number of search string in file for /F "delims=:" %%i in ('findstr /N /I /C:%searchstring% %infile%') do (set par=%%i) set /a pos=%par%+%offset% goto start :1 if %offset% LSS 0 set k=1 if %offset% GTR 0 set k=-1 if [%pos%]==[%par%] goto end set /a pos=%pos%+%k% rem displays the search string (and additional) :start for /f "usebackq delims=" %%x in (`find /n /v "" %infile% ^| find "[%pos%]"`) do (set value=%%x) echo %value% goto 1 :end
File creation «findrow.bat» has three input parameters that must always be asked:
findrow.bat %1 %2 %3
Where:
%1 — file in which you want to search a string
%2 — search string
%3 — include additional lines to (example, -3) or after (example, 3) search string
Let’s look at a more complex case, for example, the search string in the binary log of server MySQL. Need to find a command ‘DROP TABLE `mysalers`’ in the binary log mysql-bin.000001:
mysqlbinlog --database=sale "c\:MySQL\data\mysql-bin.000001" > "d:\binlog.txt" | d:\findrow.bat "d:\binlog.txt" "DROP TABLE `mysalers`" -3
output -------- [325]# at 6558 [326]#150918 10:51:09 server id 1 end_log_pos 6679 CRC32 0x22af3729 Query thread_id=1 exec_time=0 error_code=0 [327]SET TIMESTAMP=1442562669/*!*/; [328]DROP TABLE `mysalers` /* generated by server */
As a result, we obtain the desired command to delete a table `mysalers` and 3 lines going to it. These lines are also important because they contain the position of the search string and the future planned for her in the binary log. These positions can then be used when restoring the database of MySQL when you want to skip some commands.