(cmd批处理)如何在每(n)行后插入空行?

时间:2013-07-23 13:17:47

标签: batch-file cmd

我有这个脚本:

@ECHO OFF

(FOR /f tokens^=2^ delims^=^"  %%a IN ('findstr /l "\<SOMETHING=\>" FILE.vav') DO (Echo | Set /p =%%a,)) >> Results.txt

现在结果如下:

SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING, 

..但我需要在每5个结果后插入空行,因此结果应如下所示:

SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING,
//blank line
SOMETHING, SOMETHING, SOMETHING, SOMETHING, SOMETHING,
// blank line
SOMETHING, SOMETHING, SOMETHING, 

伙计们,请帮帮我吧?我没希望了.. 可能解决方案是FOR /LIF,但我无法理解。

提前谢谢你,Jakub

2 个答案:

答案 0 :(得分:3)

试试这个:

ECHO OFF &SETLOCAL 
(FOR /f tokens^=2^ delims^=^"  %%a IN ('findstr /l "\<SOMETHING=\>" FILE.vav') DO (
    <NUL Set /p =%%a,
    SET /a counter+=1
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET /a counter%%=5
    IF !counter! equ 0 ECHO(
    ENDLOCAL 
    )
)>Results.txt
TYPE results.txt

答案 1 :(得分:0)

由于标题以更一般的方式吸引提问者

比这里呈现的(Endoro完全回答)

我希望显示一个解决方案,用于在读入的文件中插入空行:

@Echo off&SetLocal EnableDelayedExpansion
Set "File=%~1" & If not defined File (Echo No file name passed&Pause&Exit /B 1)
Set n=2
for /F "delims=" %%A in (
    'Findstr "^" %FILE%'
) do (
    Echo:%%A
    set /A "#+=1,#%%=n" & If !#!==0 Echo:
)

一个非常类似的处理另一个命令的输出,
此处sc已过滤findstr

@Echo off&SetLocal EnableDelayedExpansion
Set n=2
for /F "delims=" %%A in (
  'sc query ^| findstr "SERVICE_NAME DISPLAY_NAME"'
) do (
   Echo:%%A
   set /A "#+=1,#%%=n" & If !#!==0 Echo:
)
  • 变量n不言而喻。
  • 异常但合法的变量#计算行数,模数由n划分,如果为零则发出所需的空白行。
  • 由于EnableDelayedExpansion !输出中的感叹号被删除。