-1

I need to get only the every 3rd line of a text file(Movie.txt) and put it on a separate text file(Title.txt) using batch script(for /f)

Movie.txt

 Movie
  Title
  Anime1
Movie
  Title
  Anime2
Movie
  Title
  Anime3
Movie
  Title
  Anime4

Title.txt

Anime1
Anime2
Anime3
Anime4

currently I have no idea how to get the required data... I would appreciate any help you can provide. thank you.

g00re
  • 25
  • 5

2 Answers2

1
@echo off
setlocal enabledelayedexpansion
set input=yourinput.txt
set output=youroutput.txt
set i=0
for /f "tokens=*" %%l in (%input%) do (
    set /a i=!i!+1
    set /a r=!i!%%3
    if !r!==0 echo %%l>>%output%
)

We can use the modulo operator and a line counter. !i! contains the line number and !r! is the line number modulo 3. So if !r!==0 we write the line (%%l) into the output file.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
0
@ECHO OFF
SETLOCAL
SET "line1="
SET "line2="
FOR /f "tokens=1*delims=" %%a IN (
 'findstr /r "." "100lines.txt"'
 ) DO (
 IF DEFINED line1 (
  IF DEFINED line2 (
   ECHO %%a
   SET "line1="
   SET "line2="
  ) ELSE SET line2=y
 ) ELSE SET line1=y
)
GOTO :EOF

All you need is to replace the input filename - I have a file named 100lines.txt that contains 100 different lines of text.

Since a variable's current defined status acts on the run-time value of the variable, ensure that the two flags are clear to start, then for each line of the file,

if line1 is not defined, set it
otherwise, if line2 is not defined, set it
otherwise we are on a *3 line, so regurgitate it and clear the two flags.

Magoo
  • 77,302
  • 8
  • 62
  • 84