(cmd batch), how do I insert an empty line after each (n) line?

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 is as follows:

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

.. but I need to insert an empty 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 help me guys? I am hopeless. The solution will probably be with FOR /L and IF , but I can't figure it out.

Thanks in advance, Jakub

+4
source share
2 answers

try the following:

 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 
+3
source

As the title attracts questionnaires with a more general approach

what is presented here (to which Enroro answered perfectly)

I would like to show a solution for inserting blank lines into 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 very similar, which handles the output of another command,
here is 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 self-evident.
  • An unusual but legal variable # counts lines, is a module divided by n , and if zero yields the desired empty string.
  • Due to EnableDelayedExpansion ! Exclamation marks are dropped.
0
source

Source: https://habr.com/ru/post/1492958/


All Articles