Trim a line to just a string batchfile

我的梦境 提交于 2021-02-04 21:55:27

问题


I have over 2000 of these (similar) in a text file:

2018-07-07_11_38_MA_output_log.txt:[13:00:54] Accepted authentication token of user 76561198071607345 with global ban status 0 signed by Warsaw 1 server.
2018-07-07_11_38_MA_output_log.txt:[14:07:55] Accepted authentication token of user 76561198071607345 with global ban status 0 signed by Warsaw 1 server.
2018-07-07_11_38_MA_output_log.txt:[14:49:50] Accepted authentication token of user 76561198071607345 with global ban status 0 signed by Warsaw 1 server.
2018-07-07_11_38_MA_output_log.txt:[14:51:56] Accepted authentication token of user 76561198071607345 with global ban status 0 signed by Warsaw 1 server.
2018-07-07_11_38_MA_output_log.txt:[15:35:53] Accepted authentication token of user 76561198139232244 with global ban status 0 signed by Warsaw 1 server.

I need to trim these down to just the 76561198071607345 (they are not all identical).

I also grab these all from logs using a batchfile:

cd ..
cd servers\1\logs

findstr /R 7656*  *_MA_output_log.txt >> "..\..\..\tools\pre-results.txt"

回答1:


Easy one (because of the beautiful structure of the data):

for /f "tokens=7" %a in (t.txt) do @echo %a

(this is command line syntax. For use in a batch file, use %%a instead of %a)




回答2:


I would do it the following way:

@echo off
rem // Read the text file line by line:
for /F "usebackq delims=" %%L in ("pre-results.txt") do (
    rem // Store current line:
    set "LINE=%%L"
    rem // Toggle delayed expansion to avoid loss of `!`:
    setlocal EnableDelayedExpansion
    rem // Split off file name part from string:
    set "LINE=!LINE:*:=!"
    rem // Split off time part:
    set "LINE=!LINE:*] =!"
    rem // Extract string portion of interest, but only if fixed string is found:
    for /F "tokens=6" %%K in ('cmd /V /C "echo(^!LINE^!" ^| findstr /B /C:"Accepted authentication token of user "') do (
        endlocal
        rem // Return desired string portion:
        echo(%%K
        setlocal EnableDelayedExpansion
    )
    endlocal
)

(This regards that the file name part might also contain ] and that the time part could also contain a SPACE in case the hour part consists of one digit.)



来源:https://stackoverflow.com/questions/51792364/trim-a-line-to-just-a-string-batchfile

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