(cmd batch) how do i insert blank line after every

2019-02-23 09:06发布

I have this script:

@ECHO OFF

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

Now the result looks like that:

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

..but I need to insert blank line after every 5 results, so the result should look like this:

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

Could you please help me, guys? I am hopeless.. Probably the solution will be with FOR /L and IF, but I cannot figure it out.

Thank you in advance, Jakub

2条回答
爷、活的狠高调
2楼-- · 2019-02-23 09:24

As the title attracts questioners with a more general approach

than here presented (which is perfectly answered by Endoro)

I'd like to show a solution for inserting blank lines in a file read in:

@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:
)

And a very similar one which processes the output of another command,
here sc filtered by a 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:
)
  • The variable n is self-evident.
  • The unusual but legal variable # counts lines, is modulus devided by n and if zero issues the wanted blank line inserted.
  • Due to EnableDelayedExpansion ! exclamation marks in the output are dropped.
查看更多
劫难
3楼-- · 2019-02-23 09:39

try this:

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
查看更多
登录 后发表回答