2014-07-17 18 views
0

需要幫助創建一個從txt文件中顯示一定數量的文本(例如5行文本)的批處理,但僅限於特定的關鍵詞,例如'Home',最後刪除任何複製文本顯示下面的txt內容字符串,然後刪除重複的行

所以,

搜索特定字符串,如「主頁」下面的「家」顯示不是所有的只是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.txt 

Displaying 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

+1

你是什麼意思的「重複?」這些重複行是否位於關鍵字後面的5個塊中,或者是那些在文件其他任何位置具有重複的5個行中的任何行,或者在顯示的塊內是否有重複項?你想顯示5行(除了重複的文本)或刪除重複文本後5行剩下的內容?你想顯示包含目標字符串的行嗎?包含目標的行是否計爲5的第1行?在實習中編輯會有所幫助。 – Magoo

+0

嗨Magoo我已經更新了這個問題 – Elixir

回答

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

如果您想設置打印前的行數以及之後的行數(包括目標行),那麼這些行也應該起作用。

+0

命令完美無缺,很感謝Magoo – Elixir

相關問題