将行修剪为字符串批处理文件

时间:2018-08-10 18:41:34

标签: batch-file

我的文本文件中有2000多个(类似):

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.

我需要将它们缩小为76561198071607345(它们并不完全相同)。

我还使用批处理文件从日志中获取所有这些信息:

cd ..
cd servers\1\logs

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

2 个答案:

答案 0 :(得分:2)

简单(由于数据结构优美):

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

(这是命令行语法。要在批处理文件中使用,请使用%%a而不是%a

答案 1 :(得分:0)

我将通过以下方式进行操作:

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

(这考虑到文件名部分也可能包含],并且如果小时部分由一位数字组成,则时间部分也可能包含 SPACE 。)