Read every 5th line using Batch Script
I want to make a batch program with will display every 5th line of a text file, like line no 1, 6, 11, 16 .... I tried modifying head.bat code found here: Windows batch command(s) to read first line from text file
My code is like below:
@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage
SET /a counter=0
for /f "usebackq delims=" %%a in (%1) do (
set /a testcond=(%%counter-1)%4
if "!testcond!"=="0" echo %%a
se开发者_JAVA百科t /a counter+=1
)
goto exit
:usage
echo Usage: fifth FILENAME
:exit
This code is not working. Can you please tell me what is the wrong with this code?
Seems like you need to change one line in your script as follows:
@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage
SET /a counter=0
for /f "usebackq delims=" %%a in (%1) do (
set /a "testcond=(counter-1)%%5"
if "!testcond!"=="0" echo %%a
set /a counter+=1
)
goto exit
:usage
echo Usage: fifth FILENAME
:exit
Now the script should work.
精彩评论