需要帮助创建一个批处理,该批处理从txt文件中显示一定数量的文本(例如5行文本),但只显示在特定关键字(例如“Home”)下面,并最终删除任何重复文本
所以,
搜索特定的字符串,例如“home”下面的任何文本,显示值仅为5行的所有文本,最后删除任何重复的句子
我尝试修改以下命令。
@echo OFF
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (data.txt) do (
set /a LINES=LINES+1
)
:: Print the last 10 lines (suggestion to use more courtsey of dmityugov)
set /a LINES=LINES-10
more +%LINES% < data.txtDisplaying lines from text file in a batch file
Read every 5th line using Batch Script
我不知道是否有可能删除重复的
更新
是的,该右重复行位于关键字后面的5块内。
不过,不要担心删除重复项,我主要关心的是尝试在某个字符串(例如主页)下面显示文本
我有以下命令,但不显示文本下面的所有信息,只显示一行,理想情况下,我希望调整显示的数量,例如,5行数据
setlocal EnableDelayedExpansion
rem Assemble the list of line numbers
set numbers=
set "folder=C:\test\world.txt"
for /F "delims=:" %%a in ('findstr /I /N /C:"home" "%folder%"') do (
set /A before=%%a-0, after=%%a+3
set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%folder%" ^| findstr /B "%numbers%"') do echo. %%b)batch script to print previous and next lines of search string in a text file
发布于 2014-07-18 13:51:20
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $
For %%b IN ($) DO FOR /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
SET /a before=0
SET /a after=5
SET "target=home"
SET /a count=0
SET "file=q24813694.txt"
FOR /f "delims=:" %%a IN ('findstr /i /n /L /c:"%target%" "%file%"'
) DO SET /a $!count!=%%a-%before%&SET /a count+=1
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "." "%file%"') DO (
SET "printed="
FOR /f "tokens=1,2delims==" %%m IN ('set $ 2^>nul') DO IF NOT DEFINED printed IF %%a geq %%n (
SET /a count=%%n+%before%+%after%
IF %%a geq !count! (SET "%%m=") ELSE (SET "printed=Y"&ECHO %%b)
)
)
GOTO :EOF这个例行公事应该行得通。当然,您需要设置file以适合自己;还需要设置target。
如果您想要设置打印前的行数和后面的行数(包括目标行),那么这些行也应该有效。
https://stackoverflow.com/questions/24813694
复制相似问题