How do I use the FOR batch command to find all text between two specific symbols?

自古美人都是妖i 提交于 2020-01-06 07:44:07

问题


I have a batch script I made to extract chat lines from a log on my gaming server.

The input file has text lines like this:

12/30/2015 12:42:03 : #[PLAYERNAME] Hello!

I then process each line of the log to extract the player name as follows:

for /f "tokens=2 delims=[]" %%d in ("!LINE!") do (set "NAME=%%d")

This works fine IF the player has no [] bracket characters in their name.

If their name looks like the following:

12/30/2015 12:42:03 : #[<][PLAYERNAME][>] Hello!

Or anything similar, then I only extract small parts and not their complete name of <][PLAYERNAME][>.

How could I make my FOR command account for any number of brackets that the player's name might have?

Any help would be appreciated.


回答1:


This method assume that the player name ends in "] " (right-square-braquet + space). Of course, if the player name may have these characters, the method will fail.

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (test.txt) do (
   set "LINE=%%a"
   echo LINE="!LINE!"

   call :splitName

   echo NAME="!NAME!"
)
goto :EOF

:splitName
set "NAME=%LINE:*#[=%"
set "NAME=%NAME:] =" & rem "%"
exit /B

Output:

LINE="12/30/2015 12:42:03 : #[<][PLAYERNAME][>] Hello"
NAME="<][PLAYERNAME][>"



回答2:


@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q34543549.txt"
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 SET "name=%%a"
 CALL :process
)
GOTO :EOF

:process
SET "name=%name:*#=%"
:proclp1
IF "%name:~-1%" neq "]" SET "name=%name:~0,-1%"&GOTO proclp1

SET name

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q34543549.txt containing your data for my testing.

Naturally, if the text after the name-string contains ] then it's logically impossible to decipher.




回答3:


The following code looks for everything between the first occurrence of the opening symbol SPACE#[ and the first occurrence of the closing symbol ]SPACE after that in each line of game_log.txt, and writes the found substrings as the user names into game_users.txt. Of course it fails if a user name contains the latter symbol on its own:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "INFILE=game_log.txt"
set "OUTFILE=game_users.txt"
set "LEFT= #["
set "RIGHT=] "

> "%OUTFILE%" (
    for /F "usebackq delims=" %%L in ("%INFILE%") do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:*%LEFT%=!"
        call :LEN LLEN LINE
        set "REST=%RIGHT%!LINE:*%RIGHT%=!"
        call :LEN RLEN REST
        set /A "RPOS=LLEN-RLEN"
        for /F "delims=" %%P in ("!RPOS!") do (
            echo(!LINE:~,%%P!
        )
        endlocal
    )
)
endlocal
exit /B

:LEN output input
setlocal EnableDelayedExpansion
set /A "SLEN=0"
if "%~2"=="" goto :END
set "STRING=!%~2!"
if not defined STRING goto :END
set /A "SLEN=1"
for %%I in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if not "!STRING:~%%I,1!"=="" (
        set /A "SLEN+=%%I"
        set "STRING=!STRING:~%%I!"
    )
)
:END
if "%~1"=="" (
    echo(%SLEN%
    endlocal
) else (
    endlocal & set "%~1=%SLEN%"
)
exit /B

This script determines the lengths of the substring starting after the opening symbol up to the end and the substring including the closing symbol up to the end, computes the difference between these numbers and extracts the string postion starting after the opening symbol with the length of the resulting number. This method has been chosen for the script to accept all characters, including special ones, particularly %, ! and ".



来源:https://stackoverflow.com/questions/34543549/how-do-i-use-the-for-batch-command-to-find-all-text-between-two-specific-symbols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!