2015-09-12 68 views
0

我想用一个批处理文件,以找到一个文本文件中的单词,然后我想删除该行包含单词,也可以删除一些其他线路下面的例子:如何在文本文件中查找单词并删除包含该单词和接下来两行的行?

_italic_ or **bold** 
put returns between paragraphs 
indent code by 4 spaces 
indent code by 4 spaces 
_italic_ or **bold**2 

所以结果应该是:

_italic_ or **bold** 
_italic_ or **bold**2 
+0

请提供你为你的问题所作的努力简短。提供您尝试过的代码块或类似的东西。请参阅:http://stackoverflow.com/help/how-to-ask –

+0

我只是可以用另一个替换一个词,所以这在这里没有用吗? –

回答

1
@echo off 
setlocal EnableDelayedExpansion 

rem Get the number of lines before the one that contain "returns" 
for /F "delims=:" %%a in ('findstr /N "returns" input.txt') do set /A "lines=%%a-1" 

rem Open a code block to read from input.txt and write to output.txt 
< input.txt (

    rem Read and write the first "lines" lines 
    for /L %%i in (1,1,%lines%) do (
     set "line=" 
     set /P "line=" 
     echo(!line! 
    ) 

    rem Omit the number of desired lines (3 in this example) 
    for /L %%i in (1,1,3) do set /P "line=" 

    rem Copy the rest of lines 
    findstr "^" 

) > output.txt 

rem Replace the original file by the new one 
move /Y output.txt input.txt 
+0

正如用户@Mofi在(现在已删除)注释中指示的那样,此解决方案仅删除搜索字符串的最后一次出现。必须修改代码才能删除第一个或所有的事件。 – Aacini

0

下面是该任务只用于那些线由命令FOR跳过不包含空行的输入的文本文件的工作间歇代码。

它删除所有块与3行,其中第一行包含returns

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 
set "SkipLineCount=0" 
set "OutputFile=OutputTextFile.txt" 
del "%OutputFile%" 2>nul 
for /F "usebackq eol=^ delims=" %%I in ("InputTextFile.txt") do (
    if !SkipLineCount! EQU 0 (
     set "Line=%%I" 
     set "Line=!Line:returns=!" 
     if "!Line!" NEQ "%%I" (set "SkipLineCount=1") else echo(%%I>>"%OutputFile%" 
    ) else (
     set /A SkipLineCount+=1 
     if !SkipLineCount! EQU 3 set "SkipLineCount=0" 
    ) 
) 
move /Y "%OutputFile%" "InputTextFile.txt" 
endlocal 

此批处理文件处理每个非空行。命令FOR自动跳过所有不包含任何字符的行。

注1:eol=^与命令线FOR是必要的,读取和输出也只是线包含逗号或分号否则会由命令也忽略FOR

如果当前不应跳过行,则将当前行分配给变量Line。接下来,如果该行至少包含一个returns,那么所有出现的字returns都将从该行中删除不区分大小写。

如果字符串替换为从文件读取的未修改行后,变量Line的字符串不相等,则行被忽略,因为这意味着该行至少包含一个returns

否则从输入文件中读取的行被追加到输出文件。

注2:echo%%I之间(这里使用仅含有1个或多个空格或制表符作为以其他方式与一个空间,而不是(命令回声会写为这样的情况下其当前状态输出还输入线OFF转换为输出文件,而不是从输入文件读取的空格/制表符。

在至少有一个returns的行后面的下两行也被复制到输出文件时被忽略。

作为最后一个命令,将移除了3行的已创建输出文件移到输入文本文件上。

为了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • for /?
  • if /?
  • move /?
  • set /?
  • setlocal /?

请阅读微软有关Using Command Redirection Operators的文章。

2

你可以试试这个批号:

@echo off 
set skipline=2 
set search=returns 
set File=Input.txt 
setlocal EnableDelayedExpansion 
for %%a in ("%File%") do (
    set linecount=1 
    set skip=0 
    For /f "usebackq tokens=* delims=" %%b IN (`type "%%a" ^| find /V /N ""`) do (
     set a=%%b 
     set a=!a:*]=! 
     if "!a!" NEQ "" (
      set search=!a:%search%=! 
      if "!search!" NEQ "!a!" set /a skip=%skipline%+!linecount! 
     ) 
     if !linecount! GTR !skip! echo(!a! 
     set /a linecount+=1 

    )>>tmp 
    move /Y "tmp" "%%a">Nul 
) 
EndLocal 
exit 

这是工作与包含空行和多行的文件。

更改设置skipline = 2找到您搜索的单词后需要跳过多少行。

而对于多个文件只是改变设置File = Input.txt

在当前目录扩展 文件.TXT

例子:

set File=*.txt 
相关问题