
testid=
와 사이에 있는 문자열을 추출하려고 합니다 ]
.
입력 텍스트 파일
SEVERE TEST 11/18/2019 8:00:41 AM Could not find INPUT with [testid=2345]
SEVERE TEST 11/18/2019 5:02:11 AM Could not find INPUT with [testid=12345678]
예상 출력
2345
12345678
답변1
이 시도,
@echo off
for /F "tokens=* USEBACKQ" %%F in (`findstr /I /C:"8:00:41" text.txt`) do (
set string=%%F
)
set string=%string:~68%
set string=%string:~,-1%
echo %string%
for /F "tokens=* USEBACKQ" %%F in (`findstr /I /C:"5:02:11" text.txt`) do (
set string2=%%F
)
set string2=%string2:~68%
set string2=%string2:~,-1%
echo %string2%
pause
얻을 수 있는 결과는 다음과 같습니다.
2345
12345678
Press any key to continue...
마지막 비트를 제거하려면
pause
마지막에는
pause > nul
그리고 당신이 얻을 출력은
2345
12345678
답변2
vbscript에서 Regex를 사용하여 배치 파일로 수행할 수 있습니다.
@echo off
Title Extract Data between string and char from a text file using RegExp
Set "InputFile=Test.txt"
Set "OutputFile=OutputFile.txt"
Call :ExtractData "%InputFile%" "%OutputFile%"
If Exist %OutputFile% Start "" %OutputFile%
Exit
::-----------------------------------------------------------------------------------
:ExtractData <InputFile> <OutputFile>
(
echo WScript.StdOut.WriteLine Extract("%~1"^)
echo Function Extract(Data^)
echo Dim strPattern,strResult,oRegExp,Match,colMatches
echo Data = WScript.StdIn.ReadAll
echo strPattern = "\[testid=(.+)\]"
echo Set oRegExp = New RegExp
echo oRegExp.Global = True
echo oRegExp.Multiline = True
echo oRegExp.IgnoreCase = True
echo oRegExp.Pattern = strPattern
echo set colMatches = oRegExp.Execute(Data^)
echo For Each Match in colMatches
echo strResult = strResult ^& Match.SubMatches(0^) ^& vbcrlf
echo Next
echo Extract = strResult
echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------
출력 파일은 다음과 같습니다.
2345
12345678
답변3
testid=
이 배치 파일은 각 줄의 (첫 번째) (있는 경우)와 (첫 번째) 후속 (있는 경우) 사이의 텍스트를 추출합니다. ]
단, 한 가지 예외는 식별할 수 있었습니다(참조). 답변 하단):
@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%L in (input.txt) do (
set line=%%L
set right1=!line:*testid=!
if not !line! == !right1! (
set left=!right1:~0,1!
if "!left!" == "=" (
set right2=!right1:~1!
for /f "tokens=1 delims=]" %%W in ("!right2!") do (
if not %%W == !right2! (
echo.%%W
)
)
)
)
)
setlocal enabledelayedexpansion
루프의 변수를 지능적으로 작업할 수 있습니다.for /f "tokens=*" %%L in (input.txt)
input.txt
한 번에 한 줄씩 읽고 각 줄을 인덱스 변수에 넣습니다%%L
.set line=%%L
텍스트를 조작하기 쉬운 일반 변수에 복사합니다.set right1=!line:*testid=!
변수 확장에서 문자열 대체를 수행하기 위한 구문을 사용합니다 (명확성을 위해 공백이 추가됨).% var : str1 = str2 %
- 확장이 지연되어
!
대신 사용됩니다 .%
var
는line
분명합니다.str1
이다*testid
. 는 와일드카드(패턴 일치 기호)이므로 in*
이 처음 나타나는 경우까지 모든 항목과 일치합니다 . 참고하세요.testid
line
str1
가 될 수 있지만*testid=
안타깝게도 불가능합니다.str1
을 포함하려면 가 사이의 구분 기호이기=
때문입니다.=
str1
그리고str2
.str2
null입니다.
testid
따라서 이는 in이 처음 나타날 때까지 모든 것을line
null로 바꾸고 이후의 모든 것을 반환합니다testid
.- 확장이 지연되어
- 행에 가 포함되어 있지 않으면
testid
위의 내용은 변경되지 않은 전체를 반환합니다line
. 따라서line
같으면 줄에right1
아무 것도 없습니다 .testid
서로 다른 경우 이 라인을 분석해 보세요. set left=!right1:~0,1!
에서 첫 번째(가장 왼쪽) 문자를 추출합니다right1
.if "!left!" == "="
, 그 다음의 첫 번째 문자testid
는=
이므로 라인을 찾았고testid=
계속해서 분석하고 싶습니다.set right2=!right1:~1!
첫 번째 문자를 제외하고right2
모두로 설정됩니다 .right1
즉,=
.for /f "tokens=1 delims=]" %%W in ("!right2!")
right2
첫 번째 에서 분리되어]
이전의 텍스트를]
에 넣습니다%%W
.- 그렇다면 줄에
%%W == !right2!
아무 것도 없었습니다 .]
testid=
과 를 찾았다면]
그%%W
사이에 텍스트가 있습니다. 아마도 일반 변수에 할당해야 할 것입니다.
공개: 주어진 라인
[testid=a] and [testid=b]
이 배치 파일은 다음만 찾습니다 a
. 찾지 못할 것입니다 b
. 주어진 라인
[testid<c] and [testid=d]
배치 파일은 아무것도 찾지 못합니다. 첫 번째는 testid
그것을 버립니다.