对/f使用批处理时,在文本文件中保留空行 [英] preserve empty lines in a text file while using batch for /f

查看:336
本文介绍了对/f使用批处理时,在文本文件中保留空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用批处理脚本在文本文件中查找和替换字符串.我遇到了这个答案但行以行号[] ... [17]

I have been trying to find and replace a string in a text file using batch script. I came across this answer which almost solved my issue, but the empty lines were not preserved in the output file. I have tried this answer too but lines start with line numbers []... [17]

任何扩展

Any help to to extend this answer to preserve empty lines in the output file would be appreciated. Thanks

    setlocal enableextensions disabledelayedexpansion

set "search=<Tool>"
set "replace=XYZ"

set "textFile=C:\abc.txt"

for /f "delims=" %%i in ('type "%textFile%" ^| find /v /n "" ^& break ^> "%textFile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    >>"%textFile%" echo(!line!
    endlocal
)

输出看起来像:

推荐答案

您没有足够认真地研究第二个链接的答案-它的解决方案效果很好.

You did not study the answer at your 2nd link hard enough - it has a solution that works perfectly well.

我更喜欢该技术的一种变体,该变体使用*]=替代而不是子字符串:

I prefer a variant of that technique that uses *]= replacement instead of substring:

@echo off
setlocal enableextensions disabledelayedexpansion

set "search=<Tool>"
set "replace=XYZ"

set "textFile=C:\abc.txt"

for /f "delims=" %%i in ('type "%textFile%" ^| find /v /n "" ^& break ^> "%textFile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*]=!"
    if defined line set "line=!line:%search%=%replace%!"
    >>"%textFile%" echo(!line!
    endlocal
)

但是代码没有经过优化-追加重定向使事情变慢,因为必须为循环的每次迭代打开输出文件并将文件指针定位到文件末尾.在循环外一次重定向到临时文件,然后使用MOVE将临时文件替换为临时文件,这要快得多.

But the code is not optimized - the append redirection slows things down because the output file must be opened and the file pointer positioned to the end-of-file for each iteration of the loop. It is much faster to redirect to a temporary file once, outside of the loop, and then use MOVE to replace the original with the temp.

我也更喜欢使用FINDSTR而不是FIND-它可以更好地处理长行,并且不需要管道或重定向.

I also prefer to use FINDSTR instead of FIND - it handles long lines better, and does not need a pipe or redirection.

@echo off
setlocal enableextensions disabledelayedexpansion

set "search=<Tool>"
set "replace=XYZ"

set "textFile=C:\abc.txt"

>"%textFile%.new" (
  for /f "delims=" %%i in ('findstr /n "^" "%textFile%"') do (
      set "line=%%i"
      setlocal enabledelayedexpansion
      set "line=!line:*:=!"
      if defined line set "line=!line:%search%=%replace%!"
      echo(!line!
      endlocal
  )
)
move /y "%textFile%.new" "%textFile%" >nul

说实在的,我再也不会使用纯批处理来修改文本文件了.有太多需要大量奥秘代码才能解决的极端情况.上面的代码仍然存在许多潜在的问题.例如:

Truth be told, I never use pure batch to modify text files anymore. There are too many edge cases that take a lot of arcane code to work around. There are still many potential issues with the above code. For example:

  • 搜索字符串不能包含=
  • 搜索字符串不能以*!
  • 开头
  • 替换字符串不能包含!
  • 如果搜索和/或替换同时包含"&|等毒字,替换将失败.
  • The search string cannot contain =
  • The search string cannot begin with * or !
  • The replace string cannot contain !
  • The replacement can fail if the search and/or replace contain both " as well as poison characters like &, | etc.

我使用 JREPL.BAT正则表达式查找/替换实用程序反而.它更快,更强大且功能更强大.它是纯脚本(混合批处理/JScript),可以从XP开始在任何Windows计算机上本机运行,而无需第三方的exe文件.

I use the JREPL.BAT regular expression find/replace utility instead. It is faster, more robust, and much more powerful. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward, without any need for 3rd party exe files.

例如,以下简单命令可以非常快速地查找/替换文字.

For example, the following simple command does your literal find/replace very quickly.

call jrepl "<Tool>" "XYZ" /l /f "C:\abc.txt" /o -

这篇关于对/f使用批处理时,在文本文件中保留空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆