提取物中发现线+后续行从一个文本文件 [英] Extract found line + subsequent line from a text file
问题描述
我试着写来扫描多个文件,查找字符串警告
的批处理文件。
到目前为止,我有:
Im trying to write a batch file which scans several files and looks for the string "WARNING
".
So far I have:
FORFILES -m*.f08 -C"CMD /C FINDSTR /N "WARNING" ECHO @FILE >> ListOfWARNINGS
问题一:不打印在新的一行中找到字符串
Problem one: It does not print each found string in a new line.
问题二:我怎样才能改变code,并添加如果
语句,因此,它也打印线以下行,其中警告
被发现。
Problem two: How can I change the code and add a IF
statement so that it also prints the line following the line where WARNING
was found.
推荐答案
我认为,让下一行的实际将不得不回路中的文件和放大器;内容既不FORFILES也没有找到支持这一据我所知;
I think that for getting the next line your actually going to have to loop the files & content as neither forfiles nor find support this as far as I know;
@echo off
setlocal enabledelayedexpansion
for %%f in (*.txt) do (
echo Search %%f
for /f "usebackq eol= delims=] tokens=1*" %%l in (`find /n /v "" "%%f"`) do (
set line=%%m
if !printnext!==1 (
echo.%%m >> ListOfWARNINGS
set printnext=0
) else (
if not [!line!]==[] if not !line!==!line:WARNING=X! (
echo %%m >> ListOfWARNINGS
set printnext=1
) else (
set printnext=0
)
)
)
)
下面如果没有行==行:!!!!WARNING = X
问如果行等于本身警告被替换为X.后检测到匹配
Here if not !line!==!line:WARNING=X!
detects a match by asking if a line is equal to itself after WARNING is replaced with X.
C:\null>type a.txt
line 1
M O D E L S U M M A R Y
WARNING XXXXXXX
Line after warning
NUMBER OF CBAR ELEMENTS = 18655
lorem ipsum lorem ipsum lorem ipsum
WARNING ZZZZZZZ
C:\null>x.bat
Search a.txt
C:\null>type ListOfWARNINGS
WARNING XXXXXXX
Line after warning
WARNING ZZZZZZZ
C:\null>
这篇关于提取物中发现线+后续行从一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!