DOS批处理与FIND.EXE循环剥离出来的空行? [英] DOS batch FOR loop with FIND.exe is stripping out blank lines?

查看:260
本文介绍了DOS批处理与FIND.EXE循环剥离出来的空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本DOS批处理脚本剥离出空行,尚未出现,即使我使用TYPE.exe命令将文件转换为确保文件在文件中的空行是ASCII使find命令兼容与该文件。谁能告诉我如何使这个脚本包括空行?

  @ECHO关闭
FOR / F令牌有usebackq = *%% A IN(`TYPE.exebuild.properties^ | FIND.EXE / V`)DO(
  ECHO - %% A--

暂停


解决方案

这是FOR / F的设计的行为 - 它永远不会返回空行。解决方法是使用查找或FINDSTR至preFIX行的行号。如果你能保证没有线开始行号分隔符,那么你只需设置适当的分隔符,并保持令牌1 *,但只使用第二个符号。

  :: preserve使用find空行,不承担任何行开头]
::排长被截断
FOR / F令牌= 1 * delims =]%% A有(类型file.txt的^ |查找/ N / V')做回声%%乙:: preserve空白行使用FINDSTR,不承担任何行开头:
::排长> 8191字节都将丢失
FOR / F令牌= 1 * delims =%%的A(类型file.txt的^ | FINDSTR / N^)做回声%%乙:: FINDSTR变体preserves排长队
输入file.txt的> file.txt.tmp
FOR / F令牌= 1 * delims =%%的('FINDSTR / N^file.txt.tmp')一个做回声%%乙
德尔file.txt.tmp

我preFER FINDSTR - 它是更可靠。例如,查找可以截断长行 - FINDSTR不只要它直接从文件中读取。 FINDSTR确实下降排长经由管道或重定向标准输入读时。

如果该文件可能包含与定界符开始线,那么你就需要preserve与行数preFIX整行,然后使用搜索和替换删除行preFIX。你可能想扩张延迟关闭%%一个转移到一个环境变量的时候,否则任何一个!将被破坏。但后来在循环中需要延迟扩张做搜索和替换。

使用查找,即使线路可能会启动

  :: preserve空白行]
::排长被截断
FOR / Fdelims =%%的A(类型file.txt的^ |查找/ N / V')做(
  设置LN = %% A
  SETLOCAL enableDelayedExpansion
  设置LN LN =!!*] =
  回声(!LN!
  ENDLOCAL
):: preserve空白行使用FINDSTR,即使线路可能会开始:
::排长> 8191字节被截断
FOR / Fdelims = *%%的A(类型file.txt的^ | FINDSTR / N^')做(
  设置LN = %% A
  SETLOCAL enableDelayedExpansion
  设置LN LN =:*:!=
  回声(!LN!
  ENDLOCAL
):: FINDSTR变体preserves排长队
输入file.txt的>中file.txt.tmp
FOR / Fdelims = *%%的A('FINDSTR / N^file.txt.tmp')做(
  设置LN = %% A
  SETLOCAL enableDelayedExpansion
  设置LN LN =:*:!=
  回声(!LN!
  ENDLOCAL

德尔file.txt.tmp

如果您不需要担心文件转换为ASCII,那么它是更高效的下降管道,让找到或打开FINDSTR指定为参数的文件,或通过重定向。

有是在读取过程中的另一个工作围绕着完全绕过FOR / F。它看起来很奇怪,但它是更有效的。有使用延时扩容无限制,但不幸的是它具有其他限制。

1)线路必须以&lt终止; CR>< LF>(如果你的文件类型的转换,这将不会是一个问题)

2)行必有< = 1021字节长(不计的< CR>< LF>)

3)任何尾随的控制字符从每行剥离。

4)它必须从文件中读取 - 你不能用一个管道。所以你的情况,你需要使用一个临时文件到你做ASCII转换。

  SETLOCAL enableDelayedExpansion
输入file.txt的>中file.txt.tmp
FOR / F %% N的(查找/ C / V,^<file.txt.tmp)并设置CNT = %%ñ
<file.txt.tmp(
  在(11%CNT%)/ L %% n和做(
    设置LN =
    设定/ PLN =
    回声(!LN!
  )

德尔file.txt.tmp

This DOS batch script is stripping out the blank lines and not showing the blank lines in the file even though I am using the TYPE.exe command to convert the file to make sure the file is ASCII so that the FIND command is compatible with the file. Can anyone tell me how to make this script include blank lines?

@ECHO off
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE.exe "build.properties" ^| FIND.exe /V ""`) DO (
  ECHO --%%A--
)
pause

解决方案

That is the designed behavior of FOR /F - it never returns blank lines. The work around is to use FIND or FINDSTR to prefix the line with the line number. If you can guarantee no lines start with the line number delimiter, then you simply set the appropriate delimiter and keep tokens 1* but use only the 2nd token.

::preserve blank lines using FIND, assume no line starts with ]
::long lines are truncated
for /f "tokens=1* delims=]" %%A in ('type "file.txt" ^| find /n /v ""') do echo %%B

::preserve blank lines using FINDSTR, assume no line starts with :
::long lines > 8191 bytes are lost
for /f "tokens=1* delims=:" %%A in ('type "file.txt" ^| findstr /n "^"') do echo %%B

::FINDSTR variant that preserves long lines
type "file.txt" > "file.txt.tmp"
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" "file.txt.tmp"') do echo %%B
del "file.txt.tmp"

I prefer FINDSTR - it is more reliable. For example, FIND can truncate long lines - FINDSTR does not as long as it reads directly from a file. FINDSTR does drop long lines when reading from stdin via pipe or redirection.

If the file may contain lines that start with the delimiter, then you need to preserve the entire line with the line number prefix, and then use search and replace to remove the line prefix. You probably want delayed expansion off when transferring the %%A to an environment variable, otherwise any ! will be corrupted. But later within the loop you need delayed expansion to do the search and replace.

::preserve blank lines using FIND, even if a line may start with ]
::long lines are truncated
for /f "delims=" %%A in ('type "file.txt" ^| find /n /v ""') do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  set "ln=!ln:*]=!"
  echo(!ln!
  endlocal
)

::preserve blank lines using FINDSTR, even if a line may start with :
::long lines >8191 bytes are truncated
for /f "delims=*" %%A in ('type "file.txt" ^| findstr /n "^"') do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  set "ln=!ln:*:=!"
  echo(!ln!
  endlocal
)

::FINDSTR variant that preserves long lines
type "file.txt" >"file.txt.tmp"
for /f "delims=*" %%A in ('findstr /n "^" "file.txt.tmp"') do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  set "ln=!ln:*:=!"
  echo(!ln!
  endlocal
)
del "file.txt.tmp"

If you don't need to worry about converting the file to ASCII, then it is more efficient to drop the pipe and let FIND or FINDSTR open the file specified as an argument, or via redirection.

There is another work around that completely bypasses FOR /F during the read process. It looks odd, but it is more efficient. There are no restrictions with using delayed expansion, but unfortunately it has other limitations.

1) lines must be terminated by <CR><LF> (this will not be a problem if you do the TYPE file conversion)

2) lines must be <= 1021 bytes long (disregarding the <CR><LF>)

3) any trailing control characters are stripped from each line.

4) it must read from a file - you can't use a pipe. So in your case you will need to use a temp file to do your to ASCII conversion.

setlocal enableDelayedExpansion
type "file.txt">"file.txt.tmp"
for /f %%N in ('find /c /v "" ^<"file.txt.tmp"') do set cnt=%%N
<"file.txt.tmp" (
  for /l %%N in (1 1 %cnt%) do(
    set "ln="
    set /p "ln="
    echo(!ln!
  )
)
del "file.txt.tmp"

这篇关于DOS批处理与FIND.EXE循环剥离出来的空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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